简体   繁体   中英

Injecting service to controller gets unpr error

I have unpr error when I inject my service in my controller, I research a ways to do it and I did but still no progress

this is my 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';
});

this is my 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;
    }]);

and this is my 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();

}]);

and this is all I got

Error: [$injector:unpr] http://errors.angularjs.org/1.3.5/$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20task

could anyone help me. I'm new to angular and I need to refactor my code for be able to test it, But Im stock in here. Thanks in advance.

Change:

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

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM