gpt4 book ai didi

javascript - Angular 模块错误

转载 作者:行者123 更新时间:2023-12-03 11:34:38 25 4
gpt4 key购买 nike

我尝试使用最佳代码实践编写一个 Angular 应用程序,我得到了这个:

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: '/'
});
}
]);

然后我添加了家庭 Controller :

/*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');
}
]);

和主模板目前仅包含与消息变量的绑定(bind):

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

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

知道我做错了什么吗?

最佳答案

从您的代码中我可以看到您注入(inject)了未声明的模块。

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

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

关于javascript - Angular 模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26574493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com