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?

4
  • Looks from the error that something else is causing the error. Commented Apr 22, 2015 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. Commented Apr 22, 2015 at 16:02
  • Short answer is: auto-semicolons. Long answer is: don't put opening brace on the next line. Commented Apr 22, 2015 at 16:02
  • possible duplicate of What are the rules for Javascript's automatic semicolon insertion (ASI)? Commented Apr 22, 2015 at 16:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.