Reputation: 413
My Angular code does not work when written as follows:
Here it is:
app.directive("strength", function() {
return
{
require: "superhero",
link: function(scope, element, attrs, superheroCtrl) {
superheroCtrl.addStrength();
}
}
})
However, when the return object's first curly bracket is on the same line as the return statement, the code works: Here's the code that works.
app.directive("strength", function() {
return {
require: "superhero",
link: function(scope, element, attrs, superheroCtrl) {
superheroCtrl.addStrength();
}
}
})
Am I doing something else incorrect?
How else can this be solved?
Thanks!
Upvotes: 1
Views: 859
Reputation: 123739
This is related to the syntax. In the first case, it causes automatic semicolumn insertion right after the return
statement, and the second statement of defining an object literal with key/value just becomes a syntax error unless it is assigned to.
So your statement is equivalent to:-
return; //<-- Statement terminated here
{
require: "superhero", //<-- this will be a syntax error
link: function(scope, element, attrs, superheroCtrl) {
superheroCtrl.addStrength();
}
};
The return statement is affected by automatic semicolon insertion (ASI). There is no line terminator between the return keyword and the expression allowed.
Upvotes: 3