Martin
Martin

Reputation: 795

AngularJS - updating 'self' value programmatically via a function

Sorry in advance, I am not sure the best way to describe this, or title this question. My code should hopefully explain what I am trying to achieve

I have an array of listings that is looped through in the view 'ng-repeat'. Each one can get updated individually via a function (called by clicking on a button for that listing), but that function just knows about the currently clicked on listing (it is past in as a param). The function should change the value of that listing in the view.

eg:

The JS:

.controller('searchCtrl', function($scope, $state, $stateParams, $location, $ionicPopup, Service) { 
    var self = this;
    self.listings  = response.data.items.hits.hits; //Array of listings
    ....
    self.watchlist = function(listing) { //Update individual listing value
       ....
       if (listing.in_watchlist) {
           listing.in_watchlist = 0; //want this to update the actual matching listing in 'self.listings'
       } else {
           listing.in_watchlist = 1; //want this to update the actual matching listing in 'self.listings'
       }
       ....

The HTML:

<ion-item ng-repeat="listing in search.listings">
    In watchlist: {{listing.in_watchlist}}<br/>  
    <button class="button button-block button-balanced ion-star" ng-click="search.watchlist(listing);"></button>
</ion-item>

Upvotes: 0

Views: 39

Answers (1)

Nano
Nano

Reputation: 1398

The Problem seems to be the Expression inside of the ng-click directive.

Try to remove th curly bracets in your ng-click like this: ng-click="search.watchlist(listing);"

Here is a Fiddle with a working example of what you tried:

Upvotes: 1

Related Questions