1

I am not able to update data in my controller using the service. I want to do an http.get in my service, do some processing on the data and then update the $scope of the controller. I have an angular service like below. From the answers I have read in the forums my data should be updating in the view. But thats not happening.

app.service('myService', function($http, $rootScope) {
this.selected = {
        item: '' 
}
this.getData = function(key){
    return $http.get('/myapp/stocklist/'+key);
}
this.gs = [];
this.sr = [];
this.siri=[];
var vm=this;

this.cleanData = function(response){
    for( var i=0; i<response.data.length; i++ ) {
        vm.gs.push(response.data[i].name);
        vm.sr.push(response.data[i].high);
    }                       
    vm.siri.push(vm.sr);
}
});

and here is the controller. The gs and sr variable are blanks. From what I read I dont need to use watch for this and the code below should work(I am not very clear on how to use watch as well). If this would work with watch can you please tell me how to do it.

app.controller('graph', ['$scope', '$http', 'myService', function($scope,$http, myService) {
$scope.mySelected = myService.selected;
console.log($scope.mySelected);
myService.getData($scope.mySelected).then(function(response){
    myService.cleanData(response);
    $scope.sr=myService.siri;
    $scope.gs=myService.gs;
    console.log(myService.sr);
});
}]);

I am new to angular and also maybe structuring the code in the wrong way. I would appreciate any design suggestions as well.

I am also wondering if I am using service in the right way for $http.get. I had asked a question earlier in the forum and this is what I had got as a reply. It works when I use the returned data from the service function and do data processing in the controller itself. Can you please help?

4
  • Please check this query stackoverflow.com/questions/36663503/… Commented Apr 16, 2016 at 16:04
  • If you put a console.log(response) right before your myService.cleanData(response) is there anything in the object? If so, how about a console.log(response) as the first line inside your myService this.cleanData method? Still have stuff in the object. Commented Apr 16, 2016 at 16:38
  • @sachin is it not working.? check this plnkr.co/edit/TRpa9540r8TPcMJeYhwY?p=preview it is working for me. Commented Apr 16, 2016 at 16:43
  • @sreeramu hi sreeramu, this was a mistake from my side. I was not making the server call correctly. works fine. Thanks Commented Apr 16, 2016 at 17:35

2 Answers 2

0

Looks like you're calling the myService service again to set the sr $scope after getting a response. Instead, set $scope.sr and $scope.gs to response.siri and response.gs, respectively. Updated code below:

app.controller('graph', ['$scope', '$http', 'myService', function($scope,$http, myService) {
$scope.mySelected = myService.selected;
console.log($scope.mySelected);

myService.getData($scope.mySelected).then(function(response){
    $scope.cleanData = response;
    $scope.sr = response.siri;
    $scope.gs = response.gs;
    console.log($scope.sr);
});
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

Sorry this works fine. I made a mistake in making the get call. Hence not giving the correct data. Thank you for your help :)

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.