Angular JS Uncaught Error:[$ injector:modulerr]简单代码

时间:2014-08-20 05:25:37

标签: javascript angularjs angularjs-directive

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! 
You either misspelled the module name or forgot to load it.

我已经搜索了很多关于我的问题并找不到解决方案,我敢打赌它是愚蠢的。

我的代码:

myangular.js

 (function(){

    var app = angular.module('newapp', []);

    app.controller("controller", function(){
        this.creador = "daniel";
    });

});

的index.html

<!DOCTYPE html>
<html ng-app="newapp">
<head>
    <title></title>
    <script src="bower_components/angular/angular.js"></script>
    <script src="js/myangular.js"></script>
</head>
<body>
    <div ng-controller="controller as cont">
        <strong>Hello World {{ cont.creador }}</strong>
    </div>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

我尝试了您的代码,我认为错误消息应该是newApp而不是myApp

您正在使用IIFE(Immediately-Invoked Function Expression),但您只是在定义IIFE而不是调用它。您在此IIFE中定义了newApp

(function(){

    var app = angular.module('newapp', []);

    app.controller("controller", function(){
        this.creador = "daniel";
    });

})();

或者不要使用IIFE说像bellow

var app = angular.module('newapp', []);

app.controller("controller", function(){
    this.creador = "daniel";
});

答案 1 :(得分:2)

不要使用匿名jQuery函数包装器。只需使用您的代码,它就可以正常工作。

var app = angular.module('newapp', []);

app.controller("controller", function(){
     this.creador = "daniel";
});