我花了很长时间试图弄清楚为什么我在 Angular 中遇到了 Unknown provider 错误。我已经检查了我能找到的关于该主题的所有其他问题,并且大多数都表明依赖注入存在错误。但是,在我看来,我并没有忘记注入任何东西。我一直在尝试让 resolve 属性像Misko 的这篇文章一样工作。解决后,我可以通过控制台注销员工数据,但随后出现 Unknown provider 错误,这会阻止数据显示在页面上。
这是我的路由器:
"use strict";
var app = angular.module('app',[
'employeeServices'
]);
app.config(appRouter);
function appRouter ($routeProvider) {
$routeProvider
.when('/employees/:account_id', {
controller: 'EmployeeCtrl',
templateUrl: 'view/employee/view.html',
resolve: employeeCtrl.resolve
})
.otherwise({ redirectTo: '/' });
}
这是我的控制器
var employeeCtrl = app.controller('EmployeeCtrl', [
'$scope',
'employees',
function ($scope, employees) {
$scope.employee = employees;
console.log($scope.employee);
}
]);
employeeCtrl.resolve = {
employees: function (Employee, $q, $route) {
var deferred = $q.defer();
console.log("current params: ", $route.current.params.account_id);
Employee.getOne({ id: $route.current.params.account_id }, function (successData) {
deferred.resolve(successData);
}, function (errorData) {
deferred.reject(errorData);
});
return deferred.promise;
}
};
还有我的工厂:
angular.module('employeeServices', ['ngResource'])
.factory('Employee', ['$resource', function ($resource) {
return $resource('/employees/:id/json',
{
id: '@account_id'
},
{
'save': {
method: 'POST',
isArray: false
},
'update': {
method: 'PUT',
params: {
id: '@account_id'
}
},
'remove': {
method: 'DELETE',
params: {
id: '@account_id'
}
},
'getOne': {
method: 'GET',
params: {
id: '@account_id'
},
isArray: false
},
'query': {
method: 'GET',
params: {
id: '@account_id'
},
isArray: true
}
}
);
}]);
任何建议将不胜感激!