角度模块错误

Angular module error

本文关键字:错误 模块      更新时间:2023-09-26

我尝试使用最佳代码实践编写一个有角度的应用程序,结果是:

index.html文件包含:

<!DOCTYPE html>
 <html ng-app='hrsApp'>
 <head>
<title>Hrs app</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body ng-controller="homeCtrl">
<div class='container'>
    <div ng-view></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular-route.min.js"></script>
<script src='app.js'></script>
<script src='js/controllers/homeCtrl.js'></script>
<script src='js/controllers/avCtrl.js'></script>
</body>
</html>

主文件:app.js:

angular.module('home', []);
angular.module('av', []);
// Declare app level module which depends on filters, and services
angular.module('hrsApp', [
'hrsApp.controllers',
'hrsApp.services',
'hrsApp.directives',
'hrsApp.filters',
// AngularJS
'ngRoute',
// All modules are being loaded here but EMPTY - they will be filled with controllers and      functionality
'home',
'av'
]);
// configure our routes
angular.module('hrsApp').config([
'$routeProvider',
function ($routeProvider) {
    'use strict';
    $routeProvider
        // route for the home page
        .when('/', {
            templateUrl: 'views/home.html',
            controller: 'homeCtrl'
        })
        // route for the about page
        .when('/av', {
            templateUrl: 'views/av.html',
            controller: 'avCtrl'
        })
        // route for the contact page
        .otherwise({
            redirectTo: '/'
        });
}
]);

然后我添加了家庭控制器:

/*global angular*/
angular.module('home').controller('homeCtrl', [
 '$scope',
function ($scope) {
    'use strict';
    $scope._init = function () {
        $scope.message = "welcome to Home Ctrl";
    };
    // DESTRUCTOR
    $scope.$on('$destroy', function () {
    });
    // Run constructor
    $scope._init();
    $scope.log('info', '[HomeCtrl] initialized');
}
]);

和主模板,目前只包含与消息变量的绑定:

<div>{{message}}</div>

当我尝试运行应用程序时,我得到了:未捕获错误:[$injector:modulerr]http://errors.angularjs.org/1.3.0/$injector/modulerr?p0=hrsApp&p1=错误%3A%…googleapis.com%2Fajax%2Libs%2Angularjs%2F1.3.0%2Fangular.min.js%3A18%3A3)angular.js:38

知道我做错了什么吗?

从您的代码中,我可以看到您已经注入了未声明的模块。

为了做到这一点,您必须在代码中添加以下行:

angular.module('hrsApp.controllers',[]);
angular.module('hrsApp.services',[]);
angular.module('hrsApp.directives',[]);
angular.module('hrsApp.filters',[]);