0

I want to get json data from the url and assign it to a variable. I use a service that looks like this

app.service('dataService', function($http) {
  this.getdata = function(callbackFunc) {
    $http({
        method: 'GET',
        url: '/records/json/',
     }).success(function(data){
        // With the data succesfully returned, call our callback
        callbackFunc(data);
    }).error(function(){
        alert("error");
    });
 }
});

and my controller looks like this

app.controller('ReCtrl', ['$scope', function($scope, dataService){ 
   dataService.getdata(function(dataResponse) {
      $scope.fields = dataResponse; 
   });
   ....

But I get error TypeError: Cannot read property 'getdata' of undefined. I dont know what i am doing wrong. I appreciate any help.

1 Answer 1

4

You are not injecting your service (dataService) into your controller:

app.controller('ReCtrl', 
   ['$scope', 'dataService', function($scope, dataService){ 
   dataService.getdata(function(dataResponse) {
      $scope.fields = dataResponse; 
   });
   ....

Note the extra string 'dataService' after '$scope' while defining the controller ReCtrl.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, noob mistake. I appreciate your help.

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.