AngularJS指令错误输出 - 无法读取未定义的属性'compile'

use*_*157 11 javascript angularjs angularjs-directive

新手到AngularJS并尝试创建一个简单的指令.代码失败并出现TypeError:无法读取未定义的属性'compile'.任何建议将不胜感激.

JS

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

HTML

<div ng-app="myApp">
<div my-foo></div>
</div>
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到代码和错误https://jsfiddle.net/p11qqrxx/15/

Dyl*_*att 34

这只是你的退货声明.

坏:

return 
{} // This returns undefined, return is odd and doesn't look at the next line
Run Code Online (Sandbox Code Playgroud)

好:

return{
} // Returns an empty object, always open your object on the same line as the return
Run Code Online (Sandbox Code Playgroud)

更好:

var directive = {};
return directive; // How I always do it, to avoid the issue.
Run Code Online (Sandbox Code Playgroud)