0
我unpr错误,当我在我的控制器注入我的服务,我研究一个方法可以做到这一点,我没有,但仍然没有进展喷吹服务控制器获取unpr错误
这是我app.js
'use strict';
var myApp = angular.module('myapp', [
'ngRoute','ui.bootstrap'
]);
angular.module('myapp')
.config(['$routeProvider','$locationProvider',
function($routeProvider,$locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/home.html',
controller: 'mainController'
})
.when('/home',{
templateUrl: 'views/home.html',
controller: 'mainController'
})
.otherwise({
redirectTo: '/'
});
// use the HTML5 History API
$locationProvider.html5Mode(true);
}]);
angular.module('myapp')
.run(function($rootScope){
$rootScope = 'link to api';
});
这是我service.js
'use strict';
/*
$http is neccessity param to make rest calls
$q variable to hold a promise
$rootscope handle scope
*/
angular.module('myapp')
.service('task',['$http','$q','$rootscope',function($http, $q, $rootscope){
var task = this;
task.itemList = {}
task.getItem = function(){
var defer = $q.defer();
$http.get('by_pass.php' + $rootscope.itemUrl)
.success(function (res){
task.itemlist = res;
defer.resolve(res);
})
.error(function (err, status) {
defer.reject(err);
});
return defer.promise;
}
return task;
}]);
,这是我controller.js
'use strict';
angular.module('myapp')
.controller('mainController',['$scope', '$rootScope', 'task', function($scope, $rootScope, task) {
console.log($rootScope.itemUrl);
$scope.init = function(){
$scope.perpx = 9;
$scope.itemPlaced = "0";
$scope.listSelectedItem = 1 ;
$scope.taskGetItem();
console.log('here');
}
$scope.taskGetItem = function(){
console.log('here too');
var getItemResult = task.getItem()
.then(function(res){
//when success
$scope.tasks = task.itemList;
console.log($scope.tasks);
}, function(err){
//error
});
}
$scope.init();
}]);
,这就是我的一切
Error: [$injector:unpr] http://errors.angularjs.org/1.3.5/$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20task
任何人都可以帮助我。我是新来的角,我需要重构我的代码,以便能够测试它,但我在这里库存。提前致谢。
你为什么注射服务'$ rootscope'?我建议你将'task.getItem($ rootscope.itemUrl)'作为参数传递给service方法。由于您需要在服务定义中使用__' $ rootScope'__而不是'$ rootscope'。 – Satpal 2014-12-05 06:32:23
hmm。有什么不同?那是正确的吗? *不知道 – CaffeineShots 2014-12-05 06:40:03
您需要了解[关注分离](http://deviq.com/separation-of-concerns),并确定这是前进的方向。 – Satpal 2014-12-05 07:21:22