当DWR回调更改时,Angularjs模型不会呈现

时间:2013-09-07 06:24:00

标签: angularjs dwr

尝试在DWR回调中更改'model'时遇到问题。

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.
}

任何人都有任何想法?

2 个答案:

答案 0 :(得分:2)

我对DWR并不是很熟悉,但我的猜测是你需要$ scope。$适用于封装模型更改。像这样:

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.
}

答案 1 :(得分:0)

只是为了澄清urban_racoons的回答: DWR对服务器进行异步调用。所以结果也是异步接收的。

AngularJs未检测到模型中的异步更改(引用here)。要使更改生效,您必须调用$ scope.apply()(由urban_racoons完成)。

编写上述代码的另一种方法是:

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.
}