Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  25] [ 7]  / answers: 1 / hits: 17149  / 9 Years ago, thu, april 9, 2015, 12:00:00

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/


More From » angularjs

 Answers
1

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.

[#67132] Tuesday, April 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
krystadesiraeo

Total Points: 493
Total Questions: 93
Total Answers: 100

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
;