我知道類似的有關這個錯誤的問題已經在SO中被多次提出過了,我已經閱讀了答案,但據我所知,我的代碼設計正確。但它當然不能,因爲我得到這個錯誤..我的應用程序有幾個服務和幾個控制器,並且出於某種原因除了一個特定的服務/控制器之外它們都一起工作。我已經能夠將代碼剝離到最低限度,如下所示。出現錯誤時,是的TestController從視圖觸發與.. ng-controller="TestController" ..
並在Chrome控制檯中的錯誤信息是:
Error: [$injector:unpr] http://errors.angularjs.org/1.2.15/ $injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20Loyper at Error (native)..
我一直在爲此而努力了一段時間,並不能看到這一服務(Loyper )和控制器(TestController)在注射方面的定義不是那些正在工作的控制器。任何幫助將不勝感激。
var app = angular.module('testapp', [
"ngRoute",
"mobile-angular-ui"
]);
app.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
$routeProvider.
when('/', {
templateUrl: 'main.html',
controller: 'TestController'
}).
when('/loyper', {
templateUrl: 'loyper.html',
controller: 'LoypeController'
}).
otherwise({
redirectTo: '/'
});
$httpProvider.defaults.useXDomain = true;
}]);
app.service('Loyper', ['$scope', '$http', function($scope, $http) {
var cached = false;
var loyper = {};
var message;
return {
foo: 'bar'
}
}]);
app.controller('TestController', ['$scope', 'Loyper', function($scope, Loyper) {
angular.extend($scope, {
getLoyper: function() {
return Loyper.foo;
}
});
$scope.loypelist = $scope.getLoyper();
}]);
謝謝@ jb-nizet!但是$ rootScope可以在服務中注入,或者?我很確定我看過它。順便說一下,我的服務使用$ http,我只是刪除了代碼。 – 2014-11-08 17:11:20
是的,可以在服務中注入$ rootScope。 – 2014-11-08 17:16:51