我目前正在研究一個Web應用,該應用使用後端的Django REST框架和前端的AngularJS。對於Angular來說,我還是比較新的,而且我正努力在頁面渲染完成之前讓項目列表解決。每當頁面加載時,Chromeconsole報告以下錯誤:在AngularJS中努力解決路由資源問題
Error: [$injector:unpr] Unknown provider: projectsProvider <- projects <- HomeCtrl
http://errors.angularjs.org/1.4.2/$injector/unpr?p0=projectsProvider%20%3C-%20projects%20%3C-%20HomeCtrl
at REGEX_STRING_REGEXP (http://127.0.0.1:8000/static/bower_components/angular/angular.js:68:12)
at http://127.0.0.1:8000/static/bower_components/angular/angular.js:4264:19
at Object.getService [as get] (http://127.0.0.1:8000/static/bower_components/angular/angular.js:4411:39)
at http://127.0.0.1:8000/static/bower_components/angular/angular.js:4269:45
at getService (http://127.0.0.1:8000/static/bower_components/angular/angular.js:4411:39)
at Object.invoke (http://127.0.0.1:8000/static/bower_components/angular/angular.js:4443:13)
at ident.$get.extend.instance (http://127.0.0.1:8000/static/bower_components/angular/angular.js:9001:34)
at nodeLinkFn (http://127.0.0.1:8000/static/bower_components/angular/angular.js:8111:36)
at compositeLinkFn (http://127.0.0.1:8000/static/bower_components/angular/angular.js:7543:13)
at publicLinkFn (http://127.0.0.1:8000/static/bower_components/angular/angular.js:7418:30) <div ng-view="" class="ng-scope">
這裏的路線:
var app = angular.module('projectile', [
'ngRoute',
'btford.markdown',
'projectile.controllers',
'projectile.services'
])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: '/static/templates/home.html',
resolve: {
projects: function (MultiProjectLoader) {
return MultiProjectLoader();
}
},
controller: 'HomeCtrl'
})
.otherwise({
redirectTo: '/'
});
});
這裏的控制器:
angular.module('projectile.controllers', [
'projectile.directives',
'projectile.services',
'ngRoute',
'btford.markdown'
])
.controller('HomeCtrl', function ($scope, projects, Issue) {
// Get open issues
$scope.issues = Issue.query({ open: true });
// Get projects
$scope.projects = projects;
});
這裏的服務:
angular.module('projectile.services', ['ngResource'])
.factory('Project', function ($resource) {
return $resource('/api/projects/:projectId');
})
.factory('MultiProjectLoader', function (Project, $q) {
return function() {
var delay = $q.defer();
Project.query(function(projects) {
delay.resolve(projects);
}, function() {
delay.reject('Unable to fetch projects');
});
return delay.promise;
};
});
什麼是 出錯了?我該如何解決它?如果我在控制器內使用debugger
,projects
被定義並且包含正確的數據,所以我非常難過。
我可以通過Project
並運行Project.query()
來獲得結果,但這意味着頁面在渲染後得到更新。我有一個微調指令,我想等待請求完成之前呈現頁面,我知道resolve
是。
@Claies不,這不是一個錯字。我試圖複製[O'Reilly AngularJS書中的GutHub應用程序]的功能(https://raw.githubusercontent.com/shyamseshadri/angularjs-book/master/chapter4/guthub/app/scripts/controllers/ controllers.js)。 –
我正在嘗試瀏覽整個項目存儲庫,以查看您的代碼和示例之間可能有什麼不同。 – Claies
@Claies - 項目不是服務,它是由路徑解析提供的參數 – CainBot