1

I am getting Unknown Provider

Error: [$injector:unpr] http://errors.angularjs.org/1.4.8/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20RfcDataService

with the code below. Can someone guide me what's wrong here?

    var RFC = angular.module('rfcApp', []);

    RFC.service('RfcDataService', function($scope, $http) {
      this.getUserData = function($http) {
          $http.get("http://myserver:8080/UserPrefs?jid=fcc").then(function(response) {
            return response.data;
          });
      };
      this.getHomeData = function($scope) {
        angular.forEach($scope.rfcData, function(singleItem) {
            if (singleItem.dataFor === 'Home') {
              $scope.homeTabs = (singleItem.tabs);
            }
        });
      };
    }

    RFC.controller('RfcMainController', ['$scope', '$http', '$sce', 'RfcDataService', function($scope, $http, $sce, RfcDataService) {
      $scope.rfcData = RfcDataService.getUserData($http);
      RfcDataService.getHomeData($scope);
    }

2 Answers 2

2

First:

  • Remove $http from your getUserData function - it's already in scope, and your service function is not injectible anyway.

  • if you are calling $http with query string parameters, then pass it as a second parameter

Your getUserData function should return a promise:

  this.getUserData = function() {
      return $http.get("http://myserver:8080/UserPrefs", {jid:'fcc'}).then(function(response) {
        return response.data;
      });
  };

Which you can simplify like this:

  this.getUserData = function() {
      return $http.get("http://myserver:8080/UserPrefs", {jid:'fcc'});
  };

Then use the result like this:

  RfcDataService.getUserData().success(function(result) {
      $scope.rfcData = result;
  });
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to inject $scope into service. You can't do it hence the error. Correct code for the service:

var RFC = angular.module('rfcApp', []);

RFC.service('RfcDataService', function($http) {
    this.getUserData = function() {
        $http.get("http://myserver:8080/UserPrefs?jid=fcc").then(function(response) {
            return response.data;
        });
    };
});

RFC.controller('RfcMainController', ['$scope', '$http', '$sce', 'RfcDataService', function($scope, $http, $sce, RfcDataService) {
    $scope.rfcData = RfcDataService.getUserData($http).then(function() {
        angular.forEach(data, function(singleItem) {
            if (singleItem.dataFor === 'Home') {
                $scope.homeTabs = singleItem.tabs;
            }
        });    
    }); 
}]);

Note, how you work with promise object returned by getUserData method. You also don't need $http parameter in getUserData method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.