Angularjs model does not render when change in DWR call back

拟墨画扇 提交于 2019-12-11 06:36:24

问题


I have a problem when try to change 'model' in DWR call back.

function mainCtrl($scope) {
     $scope.mymodel = "x";  // this is ok
     DWRService.searchForSomething(function(result){
           $scope.mymodel = result; // PROBLEM!!! it does not rerender the new value
     }
     $scope.mymodel = "y";  // this is also ok.
}

Anyone has any ideas?


回答1:


I'm not super familiar with DWR, but my guess is that you need an $scope.$apply to enclose your model change. Like so:

function mainCtrl($scope) {
   $scope.mymodel = "x";  // this is ok
   DWRService.searchForSomething(function(result){
       $scope.$apply(function() {
            $scope.mymodel = result; // PROBLEM!!! it does not rerender the new value
       });
   });
   $scope.mymodel = "y";  // this is also ok.
}



回答2:


just to clarify urban_racoons answer: DWR makes an Asynchronous call to the server. So the result is also received asynchronously.

Asynchronous change in model is not detected by AngularJs (reference here). To make the change effective you have to call $scope.apply() (as done by urban_racoons).

Another way to write above code is:

function mainCtrl($scope) {
     $scope.mymodel = "x";  // this is ok
     DWRService.searchForSomething(function(result){
           $scope.mymodel = result; // PROBLEM!!! it does not rerender the new value
           $scope.apply();
     }
     $scope.mymodel = "y";  // this is also ok.
}


来源:https://stackoverflow.com/questions/18670458/angularjs-model-does-not-render-when-change-in-dwr-call-back

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!