简体   繁体   中英

How to update the ng-repeat view of a list?

This simple fiddle displays a list using ng-repeat . But if I change the list from outside the DOM (in this case, using a timer), the list view does not get updated.

What do I have to do to fix this?

Because you're calling setTimeout( fn ) from outside of Angular, the digest cycle doesn't know that the variable has been updated.

Try using Angular's $timeout service so that when the function is run, Angular will know to apply the digest cycle.

Updated example

Edit: example using $scope.$apply() , in case $timeout is not what's being used.

From the comments: "Yeah, you can run if(!$scope.$$phase) $scope.$apply() . There is also $scope.$digest if you're just digesting the local scope. JSFiddle "

Code example from within controller:

$scope.lines = lines;
setTimeout(function() {
    lines.push({text: 'new text'});
    console.log('Line added: ' + lines.length);
    if ( !$scope.$$phase ) $scope.$apply();
}, 1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM