0

noob alert

This is weird - trying to create a custom directive in AngularJS, when I write this code:

myModule.directive('myTab', function(){
    console.log('--Inside TAB directive--');
    return 
    {
        template: '<div>Hello World</div>'
    };
});

It throws the exception: TypeError: Cannot read property 'compile' of undefined

However, this code runs fine:

myModule.directive('myTab', function(){
    console.log('--Inside TAB directive--');
    return {
        template: '<div>Hello World</div>'
    };
});

The only difference is the opening curly brace is on the next line in the first code. Is this behaviour normal?

kangaroo
  • 3
  • 1
  • Looks from the error that something else is causing the error. – Omri Aharon Apr 22 '15 at 16:00
  • @OmriAharon I am able to reproduce it consistently - if I move the curly brace after "return" to the next line it throws the error, else its fine. – kangaroo Apr 22 '15 at 16:02
  • Short answer is: auto-semicolons. Long answer is: don't put opening brace on the next line. – Sergio Tulentsev Apr 22 '15 at 16:02
  • possible duplicate of [What are the rules for Javascript's automatic semicolon insertion (ASI)?](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) –  Apr 22 '15 at 16:04

1 Answers1

1

because you are returning from the function and the next line is ignored. It will literally just see return, and return undefined

Joe Fitter
  • 1,309
  • 7
  • 11