3

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>"
  };
});
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Kamran G.
  • 463
  • 2
  • 5
  • 16
  • And I just learned something new about JavaScript. https://jsfiddle.net/o4ctxght/ Nice question! – David Oct 05 '16 at 12:31

3 Answers3

3

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.

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
1

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>"
  };
});
taguenizy
  • 2,140
  • 1
  • 8
  • 26
1

This is a classic case of automatic comma insertion in javascript.

When you move the bracket to the next like, the compiler presumtiuously inserts a semi colon at the end of your line, thinking you've forgotten it.

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