How to update a list inside ng-repeat using angular?


#1

i lave an ionic list

<ion-list>
  <ion-item ng-repeat="item in items">
    Hello, {{item.name}}!
  </ion-item>
</ion-list>

then, in the controller i have a event:

$scope.items = someData();
itemsRef.on('event', function (response) {
    console.log(response); // this is an object containing he "name" property
});

i would like append the response as a ion-item, without inserting html, but somehow adding the response to the items

I’ve tried: $scope.items .push(response); but weirdly enough i get Uncaught TypeError: undefined is not a function

edit: it looks like one doesn’t just $scope.items.push(response);, but $scope.items[next_key_here] = response;

jsfiddle: http://jsfiddle.net/patrioticcow/v2311770/4/


#2

Hi friend,

you should $watch your collection items. I do believe you have a function called in a ng-click, which inserts a new item in your items… so, it would be something like:

$scope.data.items = someData();

 $scope.addNewItem = function(){
     $scope,data,items.push(someNewData());
}

$scope.$watch('data.items', $scope.addNewItem (),true);