在下面的代码段中,两个变量$scope.recordList和$scope.recordList2的值完全相同。$scope.recordList2已被设置为静态值,$scope.recordList将在收到服务器响应后进行更新。语句$scope.recordList & $scope.recordList2呈现相同的值,$scope.recordList在接收响应后呈现。
最后,有两个智能表(https://github.com/lorenzofox3/Smart-Table)。$scope.recordList2基表按预期工作。基于$scope.recordList的表不像预期的那样工作。
在AngularJS中,如何在更新模型后更新视图?
<div ng-app="smartTableApp" ng-controller="smartTableController">
<script>
angular.module('smartTableApp', [ 'AngularJSRemoting','smartTableApp.controllers', 'smartTable.table']);
angular.module('smartTableApp.controllers', []).controller('smartTableController', function($scope, jsr) {
jsr.Call('appController.query', '{!soql}').then(function(response) {
$scope.recordList = response;
//$scope.$apply() ; uncommenting this gives "$digest already in progress" error
},function(err){
console.log(err);
});
$scope.recordList2=[{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6yAAA"},"Name":"Stella Pavlova","Phone":"(212) 842-5500","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6yAAA"},{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt6zAAA"},"Name":"Lauren Boyle","Phone":"(212) 842-5500","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt6zAAA"},{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt70AAA"},"Name":"Babara Levy","Phone":"(503) 421-7800","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt70AAA"},{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt71AAA"},"Name":"Josh Davis","Phone":"(503) 421-7800","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt71AAA"},{"attributes":{"type":"Contact","url":"/services/data/v30.0/sobjects/Contact/0039000000wvt72AAA"},"Name":"Jane Grey","Phone":"(520) 773-9050","CreatedDate":"2014-05-15T06:17:48.000+0000","Id":"0039000000wvt72AAA"}] ;
});
</script>
<br/>----------recordList----------<br/><!-- Shows expected values -->
{{recordList}}
<br/>----------recordList2----------<br/><!-- Shows expected values -->
{{recordList2}}
<br/><br/>
<smart-table rows="recordList" ></smart-table><br/><br/><!-- table is NOT rendered -->
<smart-table rows="recordList2" ></smart-table><br/><br/><!-- table is rendered -->
</div>发布于 2014-05-20 07:59:52
问题不在于角度没有更新视图。实际上,您在代码示例中指出它正在更新它:
<br/>----------recordList----------<br/><!-- Shows expected values -->
{{recordList}}我没有使用智能表的经验,但我确实发现本期在GitHub上报道过,它描述了一个与您类似的问题。模块的作者确实回答了这个问题,并建议用列来初始化表,以避免这个问题(他把这个问题描述为一个bug)。
因此,如果在查询之前知道数据的结构,则应该使用列数据初始化表。
https://stackoverflow.com/questions/23753668
复制相似问题