角度“Module'mainController'不可用! [英] Angular "Module 'mainController' is not available!"

查看:188
本文介绍了角度“Module'mainController'不可用!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中使用AngularJs,但我绊倒了一个错误。



这些是我包括的3个文件:

 < script src =app / lib / angular / angular.js>< / script> 
< script src =app / lib / angular / angular-route.min.js>< / script>
< script src =app / app.js>< / script>

mainCtrl.js:

  angular.module('mainCtrl',[])

.controller('mainController',function($ scope,$ http,User){
$ scope .load = true;

User.get()
.success(function(data){
$ scope.users = data;
$ scope.loading = false;
});
});

userService.js:

  angular.module('userService',[])

.factory('User',function($ http){

return {
get:function(){
return $ http.get('/ api / users / get');
}
}

}

app.js:

  var userApp = angular.module('userApp',['mainController','userService']); 



现在显示应加载用户的视图时,它会在控制台中告诉我:

  angular.js:68未捕获错误:[$ injector:modulerr]由于以下原因无法实例化模块userApp:
错误: $ injector:modulerr]无法实例化模块mainController由于:
错误:[$ injector:nomod]模块'mainController'不可用!

我添加到< body> - Tag:

  ng-app =userAppng-controller =mainController



为什么?

解决方案

这个:

  angular.module('mainCtrl',[])
pre>

  angular.module ,[])

在mainCtrl.js


I tried to use AngularJs in my Application, however I stumble upon an error.

Those are the 3 files I included:

<script src="app/lib/angular/angular.js"></script>
<script src="app/lib/angular/angular-route.min.js"></script>
<script src="app/app.js"></script>

mainCtrl.js:

angular.module('mainCtrl', [])

    .controller('mainController', function($scope, $http, User) {
        $scope.loading = true;

        User.get()
            .success(function(data) {
                $scope.users = data;
                $scope.loading = false;
            });
    });

userService.js:

angular.module('userService', [])

    .factory('User', function($http) {

        return {
            get : function() {
                return $http.get('/api/users/get');
            }
        }

    });

app.js:

var userApp = angular.module('userApp', ['mainController', 'userService']);

Now when showing the view that should load my users it tells me this in the console:

angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module userApp due to:
Error: [$injector:modulerr] Failed to instantiate module mainController due to:
Error: [$injector:nomod] Module 'mainController' is not available!

What I added to the <body>-Tag:

ng-app="userApp" ng-controller="mainController"

Why?

解决方案

Try replacing this:

angular.module('mainCtrl', [])

with this:

angular.module('mainController', [])

in mainCtrl.js

这篇关于角度“Module'mainController'不可用!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆