Push not working with option buttons


#1

For some reason when I click on my star option button the $scope.favorites is undefined thus giving me a “TypeError: Cannot read property ‘push’ of undefined”

Here is my view:

<ion-list ng-class="{'has-loading': data.isLoading}">
  <ion-item class="item item-icon-right" ng-repeat="customer in customers" type="item-text-wrap" can-swipe="true" href="#/tab/customer/{{customer.id}}" >
    {{customer.name}} 
    <i class="icon ion-chevron-right icon-accessory"></i>
    <ion-option-button class="button-positive ion-ios7-star-outline" ng-click="addFavorite({{customer}})"></ion-option-button>
    <ion-option-button class="button-positive ion-chatboxes" ng-click="showActivities({{customer.id}})"></ion-option-button>
    <ion-option-button class="button-positive ion-stats-bars" ng-click="showSales({{customer.id}})"></ion-option-button>
  </ion-item>
</ion-list>

<h5 class="fav-title-bar">My Favorite Customers</h5>

<ion-list show-delete="data.showDelete">
  <ion-item class="item item-icon-right" ng-repeat="fav in favorites" type="item-text-wrap" href="#/tab/customer/{{fav.id}}" >
    {{fav.name}} 
    <i class="icon ion-chevron-right icon-accessory"></i>
        <ion-delete-button class="ion-minus-circled" 
                         ng-click="onItemDelete(fav)">
      </ion-delete-button>
  </ion-item>
  <ion-item class="item" ng-hide="favorites.length">No Favorites</ion-item>
</ion-list>

Here is my controller part

$scope.favorites = [];

    $scope.addFavorite = function (fav){
        
        var newFav = angular.copy(fav);
        console.log($scope.favorites); // undefined ????
        $scope.favorites.push(newFav);
        
    };

#2

Figured it out. I have a promise to load favorite data and my API was returning an error then setting $scope.favorites to undefined.


#3

Glad to see you got this resolved! :smile: