Mr Kale Mr Kale - 3 months ago 10
AngularJS Question

Ng-toggle, add to array,

So I have a list of items, using

ng-repeat
. All of the items have an id which is accessbile like
{{item.id}}
when I toggle that specific item I want to push that items id to an array. I have tried numerous things but keep getting
undefined
.

This following is the current code i have tried. Thanks.

Template:

<ion-list>

<ion-toggle toggle-class="toggleClass"
ng-repeat="item in items"
ng-model="item.checked"
ng-change="updateTracker({{item.id}});"> <!--item.id didnt work either -->

<span>{{item.name}}</span> <!--{{item.id}} this works -->

</ion-toggle>

</ion-list>


Controller:

$scope.trackerList = [];
console.log($scope.trackerList);
$scope.addTrack = function() {
$scope.trackerList.push(item.id); //undefined
};
$scope.updateTracker = function(item) {
console.log($scope.item.id)

}

Answer

ngChange directive expects an expression, so you should write it like this:

ng-change="updateTracker(item.id)"

Edit:

Since you're expecting an item in your controller, change it to:

ng-change="updateTracker(item)"

$scope.updateTracker = function(item) {
  console.log(item.id)
}
Comments