[英]In AngularJS, How to update view after model is updated?
在下面的代碼片段中,兩個變量$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>
問題不在於Angular沒有更新視圖。 實際上,您在代碼示例中指出它正在對其進行更新:
<br/>----------recordList----------<br/><!-- Shows expected values -->
{{recordList}}
我沒有使用Smart-Table的經驗,但是確實在GitHub上發現了此問題 ,該問題描述了與您類似的問題。 該模塊的作者做了答復,並建議僅使用列配置來初始化表,以避免出現此問題(他將其描述為錯誤)。
因此,如果在查詢之前了解數據的結構,則應使用列數據初始化表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.