以下代码不会在每个setInterval期间呈现任何更改。但是,如果我使用addrecord推送记录,则更改将开始填充。似乎$ apply仅查找记录计数更改。有办法解决这个问题吗?如何在不添加记录的情况下重新渲染模型?
$scope.records = [
{ id:1, title:"The Shawshank Redemption", year:1994, votes:678790, rating:9.2, rank:1},
{ id:2, title:"The Godfather", year:1972, votes:511495, rating:9.2, rank:2},
{ id:3, title:"The Godfather: Part II", year:1974, votes:319352, rating:9.0, rank:3},
{ id:4, title:"The Good, the Bad and the Ugly", year:1966, votes:213030, rating:8.9, rank:4},
{ id:5, title:"My Fair Lady", year:1964, votes:533848, rating:8.9, rank:5},
{ id:6, title:"12 Angry Men", year:1957, votes:164558, rating:8.9, rank:6}
];
$scope.addRecord = function(){
$scope.records.push({
title:"New Record",
rating:999,
votes:0,
year:2013
});
};
$scope.random = function(){
$scope.records.forEach( function(v,k){
$scope.$apply(
function(){ v.votes = Math.random(); }
);
});
}
setInterval( function(){
console.log('wtf');
$scope.random( );
}, 1000);
更新:此问题是由我用于数据表的webix库引起的。我假设webix也有一个$ apply方法,需要在更新后调用。
<body ng-controller="webixTestController">
<div>
{{records}}
</div>
<div style="width:750px;">
<div webix-ui view="datatable" webix-data="records" autoheight="true" select="row">
<div view="column" id="rating" sort="int" css="rating">Rating</div>
<div view="column" id="year" sort="int">Year</div>
<div view="column" id="votes" sort="int">Votes</div>
<div view="column" id="title" sort="string" fillspace="1">Title</div>
</div>
</div>
<br>
<button ng-click="addRecord()">Add Row</button>
</body>
答案 0 :(得分:0)
http://docs.webix.com/api_link_ui.datatable_refresh.html
Webix要求在数据表上使用refresh()以使更新的模型生效。
$scope.records.forEach( function(v,k){
$scope.$apply(
function(){ v.votes = Math.random(); }
);
});
$$("table").refresh();