简体   繁体   English

angular.js:13550 TypeError:无法读取未定义的属性“编译”?

[英]angular.js:13550 TypeError: Cannot read property 'compile' of undefined?

The following Angular js code throws the error-以下 Angular js 代码抛出错误 -

"angular.js:13550 TypeError: Cannot read property 'compile' of undefined" for the code- “angular.js:13550 TypeError:无法读取未定义的属性'compile'”代码-

HelloWorld.html你好世界.html

 <!DOCTYPE html>
    <html ng-app="module1">
    <head>
        <title>My First Custom Directive</title>
        <script src="../angular.js"></script>
        <script src="helloworldDirective.js"></script>
    </head>
    <body>
        <hello-world></hello-world>
    </body>
    </html>

helloWorldDirective.js- helloWorldDirective.js-

(function() {
    
    var module1=angular.module('module1',[]);
    module1.directive('helloWorld',function(){
        return
        {
            template:'Hello Somesh!!Keep it up.'
        };
        
    });
    
    
}());

But when I replace the Javascript file with the following code it works:但是当我用以下代码替换 Javascript 文件时,它可以工作:

(function() {
    
    var module1=angular.module('module1',[]);
    var helloWorld=function()
{
var directive={};     
directive.template='Hello Somesh!!Keep it up.';
return directive;
}
module1.directive('helloWorld',helloWorld);
    
}());

Both codes are doing basically the same thing but one is failing.两个代码都在做基本相同的事情,但一个失败了。 Any thoughts?有什么想法吗?

In the first example, there is an "unreachable code error" .在第一个示例中,存在“无法访问的代码错误”

You can fix this error with:您可以使用以下方法修复此错误:

return template = {template: 'Hello Somesh!!Keep it up.'};

Else you can't get the pointer to the attribute.否则,您将无法获得指向该属性的指针。

JavaScript's Automatic Semicolon Injection turns this: JavaScript 的自动分号注入变成了这个:

  return
        {
            template:'Hello Somesh!!Keep it up.'
        };

into this:进入这个:

  return;
        {
            template:'Hello Somesh!!Keep it up.'
        };

a return, followed by a useless code block.一个返回,后跟一个无用的代码块。 (The template: is treated as a label.) template:被视为标签。)

Debugging hint: JSHint or JSLint would have found this error for you.调试提示:JSHint 或 JSLint 会为您找到此错误。

Style hint: In JavaScript, always keep open braces on the same line... though only the return and throw statement are affect by this particular issue.样式提示:在 JavaScript 中,始终在同一行上保留左大括号……尽管只有returnthrow语句会受到此特定问题的影响。

Use this method to define your directives:使用此方法来定义您的指令:

 app.directive('helloWorld', [function () {
     return {
       restrict: 'E',
       transclude: false,
       template: 'Hello Somesh!!Keep it up.'
    }
}]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular.js:13550 TypeError:无法设置未定义的属性“people” - angular.js:13550 TypeError: Cannot set property 'people' of undefined 即时通讯收到错误。 angular.js:13550 TypeError:无法读取未定义的属性&#39;$ invalid&#39; - im getting error. angular.js:13550 TypeError: Cannot read property '$invalid' of undefined angular.js:12520 TypeError:无法读取未定义的属性&#39;post&#39; - angular.js:12520 TypeError: Cannot read property 'post' of undefined angular.js:14110 TypeError:无法读取undefined的属性&#39;push&#39; - angular.js:14110 TypeError: Cannot read property 'push' of undefined Angular.js:10671 TypeError:无法读取未定义的属性 - Angular.js:10671 TypeError: Cannot read property of undefined angular.js:12520 TypeError:无法读取r。$ scope.insertvalue处未定义的属性&#39;option1&#39; - angular.js:12520 TypeError: Cannot read property 'option1' of undefined at r.$scope.insertvalue Angular.js $ http.post TypeError:无法读取未定义的属性“data” - Angular.js $http.post TypeError: Cannot read property 'data' of undefined 我不断收到此错误“ angular.js:TypeError:无法读取未定义的属性&#39;pages&#39;” - I keep getting this error “angular.js:TypeError: Cannot read property 'pages' of undefined” Angular.JS Uncaught TypeError:无法分配给只读属性 - Angular.JS Uncaught TypeError: Cannot assign to read only property angular.js:13920 TypeError:无法读取null的属性&#39;push&#39; - angular.js:13920 TypeError: Cannot read property 'push' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM