how to refresh entire ng-repeat template after list.push

5,535 views
Skip to first unread message

Adam Łepkowski

unread,
Mar 13, 2013, 9:37:18 AM3/13/13
to ang...@googlegroups.com

Hello, I have following issue I can't refresh ng-repeat template after I 've added new item to the list. Sample is placed here: http://jsfiddle.net/alepkowski/fqnKt/35/

To test the "wrong" behavior please turn on console window in firebug or other tool. When the page is loaded then in the console you can find 4 data it means that callMe function was executed 4 times, now when you add new value callMe function is executed only once for the new value but I want to execute it 5 times, How can I refresh entire ng-repeater template?

 

Thanks in advance

Clint Checketts

unread,
Mar 13, 2013, 9:56:29 AM3/13/13
to ang...@googlegroups.com
You are running into an Angular performance feature.

Essentially Angular can see that the element in the array ('A' for example) is the same object reference, so it doesn't call ng-init again. This is efficient. Even if you concatenated an old list into a new list, Angular would see that it it the same reference.

If instead you create a new object with the same values as the old object, it has a different reference and Angular re-inits it:
Bad example that does what you are looking for: http://jsfiddle.net/fqnKt/37/

$scope.add = function(item) {
        var newItems = [];
        angular.forEach($scope.items, function(obj){
            this.push({val:obj.val});
        },newItems)
        
        newItems.push({val:item})
        $scope.items = newItems;
    };

I don't recommend the approach taken in the fiddle, but rather you should find a different method that ng-init to trigger your code.




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Adam Łepkowski

unread,
Mar 13, 2013, 10:20:02 AM3/13/13
to ang...@googlegroups.com

Ok thank you for this solution. Unfortunately I need to use this bad approach because I must create a list of items, then user can pushes edit button and the edit window will appear from the left and from the right should be list of items on which edited item is in preview mode. The edited item is copy of presented item and only when the user pushes Save button, item must be saved and I don’t see any other way to resolve it. Look at SetPreview function here http://jsfiddle.net/alepkowski/fG7yd/13/

 

P.S This subject is contacted with following question if you have account on stackoverflow then you can answer if not I will make answer later.  http://stackoverflow.com/questions/15355122/angularjs-ngrepeat-with-nginit-ngrepeat-doesnt-refresh-rendered-value

Reply all
Reply to author
Forward
0 new messages