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?

Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
sachin
  • 606
  • 1
  • 6
  • 17
  • Please check this query http://stackoverflow.com/questions/36663503/invoke-one-controller-from-another/36663536?noredirect=1#comment60919252_36663536 – user3045179 Apr 16 '16 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. – Lex Apr 16 '16 at 16:38
  • @sachin is it not working.? check this http://plnkr.co/edit/TRpa9540r8TPcMJeYhwY?p=preview it is working for me. – sreeramu Apr 16 '16 at 16:43
  • @sreeramu hi sreeramu, this was a mistake from my side. I was not making the server call correctly. works fine. Thanks – sachin Apr 16 '16 at 17:35

2 Answers2

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);
});
}]);
Chris
  • 91
  • 1
  • 7
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 :)

sachin
  • 606
  • 1
  • 6
  • 17