向控制器注入服务会获得unpr错误

时间:2014-12-05 06:30:14

标签: javascript angularjs

当我在控制器中注入服务时,我发现了无错误,我研究了一些方法,我做了但仍没有进展

这是我的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
谁能帮助我。我是角色的新手,我需要重构我的代码才能测试它,但我在这里库存。提前谢谢。

1 个答案:

答案 0 :(得分:1)

变化:

.service('task',['$http','$q','$rootscope',function($http, $q, $rootscope){

要:

.service('task',['$http','$q','$rootScope',function($http, $q, $rootScope){