I'm getting unexpected End of expression error when I pass nodejs data to to ng-init. I'm confident I'm doing it right however I'm getting the error message above
I call ng-init like so:
<body id="myPage" ng-app="myApp" ng-controller="arrCtrl" ng-init="items = <%- JSON.stringify(myitems) %>">
and the error is here
Unexpected end of expression: items = [{
Thus when I run
{{items}}
It'll display as is.
I also went ahead and declared my module in my script tag just in case like so
<script>
var app = angular.module('myApp', []);
app.controller('arrCtrl', function(){
console.log("Controller loaded");
});
</script>
I looked everywhere and no one seemed to have solved this issue.
Related
I am new to angular JS. Please take a look. This is my app.js file
angular.module("CrudDemoApp",["CrudDemoApp.controllers","ngRoute"]);
this is my controllers.js file
angular.module("CrudDemoApp.controllers", []);
controllers("MainController", function ($scope)
{
$scope.message = "Main Controller";
});
this is the my body part
<body ng-app="CrudDemoApp">
<div ng-controller="MainController">
{{message}}
</div>
</body>
firstly it is saying "Uncaught ReferenceError: controllers is not defined
at then it is saying
Error: [ng:areq] http://errors.angularjs.org/1.5.6/ng/areq?p0=MainController&p1=not%20a%20function%2C%20got%20undefined
angular.module("CrudDemoApp.controllers", []) //remove semicolon here adding the controller to this module
.controller("MainController", function ($scope) // dot before controller, remove "s"
{
$scope.message = "Main Controller";
});
notice the dot and lack of pluralization on controller
You should have probably seen some other errors before the ones you mentioned always work from the first one down sometimes 1 error causes the next.
if I try to compile an EJS template like below:
var data = { case: 'Something' };
var html = ejs.render('Case <%= case %>', data);
I get the error:
ejs.js:550 Uncaught SyntaxError: Unexpected token case while compiling ejs
If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
at new Function (<anonymous>)
at Template.compile (ejs.js:550)
at Object.compile (ejs.js:359)
at handleCache (ejs.js:202)
at Object.exports.render (ejs.js:385)
at VM546 script.js:2
It's because case is a reserved word. Is there a way around it, given that I cannot change the JSON I'm feeding into EJS?
Edit: here's a link to an online Plunker - https://plnkr.co/edit/WBpjom
It looks like EJS, also set's a context called locals that is passed..
So..
var html = ejs.render('Case <%= locals.case %>', data)
Just made a simple controller with some injection.
var SimpleProductListController = BaseController.extend({
_notifications:null,
_productsModel:null,
init:function($scope,ProductsModel,$route){
},
defineListeners:function(){
this._super();
},
destroy:function(){
}
})
/...
SimpleProductListController.$inject = ['$scope','ProductsModel','$route'];
The console error points to this:
http://errors.angularjs.org/1.2.16/ng/areq?p0=SimpleProductListController&p1=not%20aNaNunction%2C%20got%20undefined
Argument 'SimpleProductListController' is not aNaNunction, got undefined
How am I supposed to even debug this? I got batarang but it does nothing here.
Basically, Angular is saying that SimpleProductListController is undefined.
When I've gotten that error, it was because I created a controller and tried to inject it into my app but I did not load the file that defines that controller by adding the script tag to my index.html file.
I'm trying to update some texts on a page that is part of $scope. But I keep getting this error:
Error: [$rootScope:inprog] [http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply][1]
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:6:450
at m (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:101:443)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:301)
at h.$scope.changeLang (http://treenovum.es/xlsmedical/js/medical-app.js:80:16)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:169:382
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:390
at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:40)
at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:108:318)
at HTMLAnchorElement.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js:186:372)
Obviously I'm doing something wrong. :)
Any ideas of how I can fix this? I want the page to update to the new variables in the scope.
This is the code I'm using for updating:
medicalApp.controller('MainCtrl', function($scope, $cookies, getTranslation) {
getTranslation.get(function(data){
$scope.translation = data;
});
$scope.changeLang = function (lang) {
console.log(lang);
$cookies.lang = lang;
$scope.$apply(function(){
getTranslation.get(function(data){
$scope.translation = data;
console.log(JSON.stringify($scope.translation));
});
});
};
});
the html:
<body ng-controller="MainCtrl">
...
<div class="header-lang col-xs-4">
<p>
DE |
FR</p>
<div>{{ translation.text }}</div> <---- Example variable I want updated.
...
I'm also using ngRoute with separate controllers for each page I load, if that has anything todo with it?
If your template don't change after changing models and you need using $scope.$apply, You can use $scope.$applyAsync instead of this.
https://github.com/angular-ui/ui-codemirror/issues/73
You are using $scope.$apply(...) inside the function changeLang so you are getting the common 'already in a digest' error. You don't need to put the call to getTranslation inside a $scope.$apply(...) block because ng-click already has you taken care of. Yank that out and it should just work. Also, I'd recommend running with a non-minified version of angular for dev so you can see better errors in your console.
$evalAsync works great:
$scope.$evalAsync(function() {
// Code here
});
Or simply:
$scope.$evalAsync();
See: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$evalAsync
I'm using backbone.js for a rails project I've got on the side. I'm embedding templates in the rails views using something like:
<script type="text/template" id="this-is-the-id">
... template goes here ...
... the line below this is where my error's coming from ...
<%% _(<%= aNumber %>.times(function(){ console.log('hi'); });) %>
</script>
I'm getting the error Uncaught SyntaxError: Unexpected token <. If I replace <%= aNumber %> with an integer, it works as expected. Also, if I console.log(<%%= aNumber %>), it logs the correct number.
Does anybody know how I can accomplish something like this? I know that's kind of a sloppy way to organize my code, but this is just a quick little hack I'm putting together.
Thanks!
Turns out I didn't need to wrap aNumber within the escaping <%%= and %>, and that fixed it for me.