3

I have select tag the options which will come from the service, after adding one more item to scope from the other service i am not getting the newly inserted value in select options, i am able to see the option after refreshing of the page, scope is not fetching the results on changing of its values. here is my code

html code is here

<select ng-model="holidaySelected"
    ng-options="opt as opt.holidayName for opt in list_of_holidaytypes">
</select>

js code is here

$http({
    method : 'get',
    url : 'url'//url is here
}).then(function(response) {
    $scope.list_of_holidaytypes = response.data;
});

after another service i am calling this service again but I am not able to see the new option in UI side.

ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
  • past also your api response. `$http({ method : 'get', url : 'url'//url is here }).then(function(response) { console.log(JSON.stringify(response.data)); $scope.list_of_holidaytypes = response.data; });` – AlainIb May 19 '16 at 08:10

4 Answers4

0

The code is perfect. I feel something fishy with the API response. Can you tell what is the response you are getting from the API. It should be as follows,

$scope.list_of_holidaytypes = [{holidayName:'Name1' }, {holidayName:'Name2' }];
Venu prasad H S
  • 231
  • 1
  • 8
  • response is same as you mention, after adding one more item into the scope variable it is able to add ( i have seen from the console) some times it is adding to the scope variable some times not, may be problem with Service call. thanks for the response Venu.. :) – Venkataramana Naik Palthya May 19 '16 at 09:06
  • The whole question is very simple. The only possible reason of failure is API response. Please recheck it once again. Or please provide the response here. – Venu prasad H S May 20 '16 at 03:32
0

It seems that you are doing $scope.list_of_holidaytypes = response.data; in service while as per angular docs, you can not access $scope inside angular service.

My suggestion is to put the code (which is responsible to fill $scope.list_of_holidaytypes) in the controller.

For example:

Controller Code:

function MyCntrl($scope, MyService) {
  function successCallback(response) {
     $scope.list_of_holidaytypes = response.data;
  }

  function failureCallback(response) {
    $scope.list_of_holidaytypes = null;
  }

  $scope.fetchList = function() {
     MyService.fetchList(successCallback, failureCallback); 
  }
}

Service Code:

function Myservice($http) {
  this.fetchList = function(successCallback, failureCallback) {
    $http({
      method : 'get',
      url : 'url'//url is here 
    }).then(function(response) {
      successCallback(response);
    }, function(response) {
      failureCallback(response);
    });  
  }
}

It should work perfectly.

Suneet Bansal
  • 2,664
  • 1
  • 14
  • 18
0

I Found the solution, what I am doing to update the scope variable in UI, I am Calling the service to get the latest scope variable value in side of the service.. here is my code

to update initial state

    $scope.OnloadServiceData = function(){
           $http({
           method : 'get',
           url : 'url'//url is here
           }).then(function(response) {
           $scope.list_of_holidaytypes = response.data;
    });
};
$scope.OnloadServiceData();

another controller

 $http({
    method : 'post',
    url : 'url'//this is another service
}).then(function(response) {
    $scope.xyz= response.data;
    $scope.OnloadServiceData();//calling the service to fetch result in UI
});
-1

try this

before call

var vm= this; 

after call user

 vm.yourevent;

use

Nikhil Vaghela
  • 222
  • 1
  • 14