Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs bracket after "return" [duplicate]

Why doesn't the angular directive work when bracket follow under return?

app.directive("alert", function () {
  return 
  {
     restrict: "EA",
      template: "<div>Alert!!</div>"
  };
});

But does work when the bracket is adjacent to the return:

app.directive("alert", function () {
  return {
     restrict: "EA",
      template: "<div>Alert!!</div>"
  };
});
like image 529
Kamran G. Avatar asked May 22 '26 14:05

Kamran G.


2 Answers

Because when you just have return that considered to be return nothing undefined(nothing). You must return something on same line otherwise its same as that of return;

& when you have it on same line it considered as you returned object(DDO) from directive.

like image 167
Pankaj Parkar Avatar answered May 24 '26 03:05

Pankaj Parkar


That's because JavaScript will take return as a single operation, this because return by itself is a valid operation.

First example would be the same thing as having

app.directive("alert", function () {
  return;
  {
     restrict: "EA",
      template: "<div>Alert!!</div>"
  };
});
like image 38
taguenizy Avatar answered May 24 '26 04:05

taguenizy