javascript - angularjs getting data from a url -


i want json data url , assign variable. use service looks this

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

and controller looks this

app.controller('rectrl', ['$scope', function($scope, dataservice){     dataservice.getdata(function(dataresponse) {       $scope.fields = dataresponse;     });    .... 

but error typeerror: cannot read property 'getdata' of undefined. dont know doing wrong. appreciate help.

you not injecting service (dataservice) controller:

app.controller('rectrl',     ['$scope', 'dataservice', function($scope, dataservice){     dataservice.getdata(function(dataresponse) {       $scope.fields = dataresponse;     });    .... 

note string 'dataservice' after '$scope' while defining controller rectrl.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -