范围变量在从服务调用更改后不会影响UI

时间:2016-05-19 06:12:20

标签: javascript angularjs

我已选择标记将来自服务的选项,在从另一个服务添加一个项目到范围后我没有在选择选项中获得新插入的值,我能够在刷新之后看到该选项页面,范围不是在更改其值时获取结果。这是我的代码

html代码在这里

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

js代码在这里

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

经过另一项服务我再次调用此服务,但我无法在UI端看到新选项。

4 个答案:

答案 0 :(得分:0)

代码很完美。我觉得API响应有点可疑。你能说出你从API得到的回应是什么吗?它应该如下,

with summary as (
    select cast(p.pri as varchar(max)) + '-' + cast(p.sec as varchar(max)) as tuple, 
        p.sec, m1.number + m2.number as number, 1 as lvl
    from t1_pivot p 
    inner join t1_main m1 on m1.id = p.pri 
    inner join t1_main m2 on m2.id = p.sec

    union all

    select s.tuple + '-' + cast(p.sec as varchar(max)) as tuple, 
        p.sec, s.number + m.number as number, lvl + 1 as lvl
    from summary s 
    inner join t1_pivot p on p.pri = s.sec
    inner join t1_main m on m.id = p.sec
)
select * from (
    select * from summary
    union select cast(id as varchar(max)) as tuple, 0 as sec, number, 0 as lvl from t1_main
) x
where x.number > 10
order by lvl, tuple

答案 1 :(得分:0)

好像你在做$ scope.list_of_holidaytypes = response.data;在服务中,而根据角度文档,您无法访问角度服务内的$ scope。

我的建议是将代码(负责填充$ scope.list_of_holidaytypes)放在控制器中。

例如:

控制器代码:

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); 
  }
}

服务代码:

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);
    });  
  }
}

它应该完美无缺。

答案 2 :(得分:0)

我找到了解决方案,我正在做什么来更新UI中的范围变量,我正在调用服务以获取服务端的最新范围变量值..这是我的代码

更新初始状态

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

另一个控制器

 $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
});

答案 3 :(得分:-1)

试试这个

致电前

var vm= this; 

致电用户

 vm.yourevent;

使用