我跟着角文檔創建我的服務,看到一個錯誤信息「未知的提供程序錯誤」
http://docs.angularjs.org/error/$injector:unpr?p0=$scopeProvider%20%3C-%20$scope%20%3C-%20$users
這是我的代碼:
var angularApp = angular.module("myApp", []);
angularApp.run(function($http) {
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
$http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});
angularApp.service('$users', function($scope, $http, $cookie, $window) {
// need to instantiate the service
var newUsersServiceInstance;
// upper case is resource
var User = $resource('/user/:userId', {userId:'@id'});
var MyPassword = $resource('/mypasswords/new');
// lower case is instance you tend to want the instance
// to be binded to the controller's scope
$scope.user // = User.get({userId:123});
$scope.getUser = function(id, s) {
$scope.user = User.get({"userId": id}, s, e);
}
$scope.changePassword = function(data, s, e) {
MyPassword.post(data, s, e);
}
// need to return the instance
return newUsersServiceInstance;
});
angularApp.controller('passwordController', ['$scope', '$users', function($scope, $users) {
$scope.changePasswordForm = function() {
$users.changePassword(
$.param($scope.data),
function(data, xhr) {
console.log(data);
},
function(data, xhr) {
console.log(data);
// error callback
$scope.errors = data.errors;
})
}
$scope.debug = function() {
console.log($scope);
}
}]);
我不知道我出錯了。
我的新代碼考慮到每個人的答案。同樣的錯誤。
var angularApp = angular.module("myApp", []);
angularApp.run(function($http) {
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
$http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});
angularApp.service('$users', function($http, $cookie, $window) {
// upper case is resource
var User = $resource('/user/:userId', {userId:'@id'});
var MyPassword = $resource('/mypasswords/new');
// lower case is instance you tend to want the instance
// to be binded to the controller's scope
this.user // = User.get({userId:123});
this.getUser = function(id, s) {
this.user = User.get({"userId": id}, s, e);
}
this.changePassword = function(data, s, e) {
MyPassword.post(data, s, e);
}
return this;
});
angularApp.controller('passwordController', ['$scope', '$users', function($scope, $users) {
$scope.changePasswordForm = function() {
$users.changePassword(
$.param($scope.data),
function(data, xhr) {
console.log(data);
},
function(data, xhr) {
console.log(data);
// error callback
$scope.errors = data.errors;
})
}
$scope.debug = function() {
console.log($scope);
}
}]);
即使更改爲使用工廠後,我看到相同的錯誤。
var angularApp = angular.module("myApp", []);
angularApp.run(function($http) {
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
$http.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
});
angularApp.factory('$users', function($resource, $http, $cookie, $window) {
// need to instantiate the service
var newUsersServiceInstance = {};
// upper case is resource
var User = $resource('/user/:userId', {userId:'@id'});
var MyPassword = $resource('/mypasswords/new');
// lower case is instance you tend to want the instance
// to be binded to the controller's scope
newUsersServiceInstance.user // = User.get({userId:123});
newUsersServiceInstance.getUser = function(id, s) {
newUsersServiceInstance.user = User.get({"userId": id}, s, e);
}
newUsersServiceInstance.changePassword = function(data, s, e) {
MyPassword.post(data, s, e);
}
// need to return the instance
return newUsersServiceInstance;
});
angularApp.controller('passwordController', ['$scope', '$users', function($scope, $users) {
$scope.changePasswordForm = function() {
$users.changePassword(
$.param($scope.data),
function(data, xhr) {
console.log(data);
},
function(data, xhr) {
console.log(data);
// error callback
$scope.errors = data.errors;
})
}
$scope.debug = function() {
console.log($scope);
}
}]);
你清除緩存,畢竟更新的也沒有辦法,你可以得到'$ scopeProvider'錯誤,'$可能發生這種情況範圍「不再被引用。 – TheSharpieOne
失去'$ cookie' ... – calebboyd
你說得對。現在是$ cookieProvider錯誤。抱歉。我已經刪除了$ cookie和$窗口。它現在的作品 –