12

Newbie to AngularJS and trying to create a simple directive. The code fails with a TypeError: Cannot read property 'compile' of undefined. Any suggestions would be greatly appreciated.

JS

var xx = angular.module('myApp', []);
xx.directive('myFoo', 
             function(){
                 return 
                 {
                     template:'23'
                 };                     
});

HTML

<div ng-app="myApp">
<div my-foo></div>
</div>

You can find the code and error here https://jsfiddle.net/p11qqrxx/15/

2
  • AngularJS 1.0.0, really? Commented Apr 9, 2015 at 18:30
  • New to AngularJS. Is this syntax angularjs 1.0? I am using AngularJS 1.2 Commented Apr 10, 2015 at 3:36

4 Answers 4

36

It's just your return statement.

Bad:

return 
{} // This returns undefined, return is odd and doesn't look at the next line

Good:

return{
} // Returns an empty object, always open your object on the same line as the return

Better:

var directive = {};
return directive; // How I always do it, to avoid the issue.
Sign up to request clarification or add additional context in comments.

5 Comments

could you please explain with fiddle?
This explains the basics of what i'm saying: jsfiddle.net/L6kmcbnL . The issue here has nothing to do with angular, really, just how javascript works. .directive expects an argument of a function that returns a configuration object for a directive. The original question's function returns undefined rather than the config object.
reproduce it with directive..I'll appreciate it
This is the best I can do without wasting a massive amount of time: jsfiddle.net/h3oyzktj . It doesn't demo it great, the whole return thing is a syntax error, which breaks the JS, so its not possible to demo both working and not at the same time. If you put the brace on line 8 beside the return on line 7, everything works. The best I can do for now. Ignore the settimeout manual boostrapping of two apps, I was trying to get them to both work and be broken at the same time.
It was the return...Can't believe that I spent close to 3 hours on this. Once I fixed the return statement it started working.
2

This is not a issue of Angular but its the way javascript return syntaxes are written and executed. I have created a simple video which demonstrates this issue in more detail. You can see this video from this link.

https://www.facebook.com/shivprasad.koirala/videos/910570055693818/

Now for the long answer. In javascript "return" and "return ;" are one the same and "{}" is a anonymous function.

When you write return and "{" in the next line its two statements one return and the "{}" is a anonymous function.Program returns from the "return" syntax and the code inside the curly bracket is never executed or we can say its a unreachable code.So it returns back "undefined".

return  // returns from this step itself
{
  // any code here will never execute
 // unreachable code
}

When you write the curly bracket just after return statment it treats them as one block of code and also executes the code inside the curly bracket.

return{
// any code here will execute
}

So its all about where your curly bracket is after the return statement.

Comments

0

You also referred AngularJs 1.0.0 which was too old, I updated it to 1.1

Change directive to this

xx.directive('myFoo',

function () {
    return {
        restrict: 'A', //defaults its A no need of it to declare.
        template: '23'
    };
});

Working Fiddle

Comments

0

xx.directive('myFoo',

function () { var obje={ restrict: 'A', //defaults its A no need of it to declare. template: '23' return obje; } });

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.