1

尝试在 DWR 回调中更改“模型”时遇到问题。

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

有人有什么想法吗?

4

2 回答 2

2

我对 DWR 不是很熟悉,但我猜你需要一个 $scope.$apply 来包含你的模型更改。像这样:

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.
}
于 2013-09-07T06:28:00.460 回答
0

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

AngularJs 未检测到模型中的异步更改(参考此处)。要使更改生效,您必须调用 $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.
}
于 2016-08-18T16:08:55.310 回答