我使用以下代码创建一个 Angular 应用程序,使用路由和 Controller 。
(function() {
angular.module('eCommerceApp', ['ngRoute'])
.config('$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/phonelist.html',
controller: 'mobilePhoneListController'
}).
when('/phone/:phoneSlug', {
templateUrl: 'partials/phonedetail.html',
controller: 'mobilePhoneDetailController'
}).
otherwise({
templateUrl: 'error/404.html',
});
})
.controller('mobilePhoneListController', ['$http', function($http) {
var thisObj = this;
thisObj.mobilePhones = [];
$http.get('/api/getallphones').then( function(data) {
thisObj.mobilePhones = data;
}, function(data) {
thisObj.mobilePhones = data || "Request Data Fail!";
});
}])
.controller('mobilePhoneDetailController', ['$http', function($http) {
var thisObj = this;
}])
})();
在此之前我导入了 3 个脚本。 Angular、Angular-route 和我的应用程序
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.min.js"></script>
<script src="/angular/e-commerce-app.js"></script>
以及网站主要结构
<html lang="en" ng-app="eCommerceApp">
<!-- ... -->
<body ng-view>
</body>
</html>
还尝试过 <ng-view></ng-view>
但我总是想得到this error .
Error: $injector:modulerr Module Error Failed to instantiate module eCommerceApp due to: Error: [ng:areq] http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20... at Error (native) at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js:6:416 ...
最佳答案
在您的网站主结构 (index.html) 中,使用 ng-app="eCommerceApp"作为“body”标记中的属性。例如:
<body ng-app="eCommerceApp">
关于javascript - AngularJS 上不断出现注入(inject) modulerr 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34369438/