Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value is in array for ng-if within ng-repeat

I have an ng-repeat looping through a list of wines retrieved from an API. I also have a array variable containing all wine ids that have been added to favorites fetched from database. I want to be able to display an "Add To Favorites" button if the user has not yet added a specific result wine from the list. To do this I thought I would do something like:

HTML:

<tr ng-repeat="wine in wines">     <td>{{$index+1}}</td>     <td>{{ wine.Name }}</td>     <td>{{ wine.Appellation.Name }}</td>     <td>${{ wine.PriceMin }} - ${{ wine.PriceMax }}</td>     <td>         <!-- If wine.Id is not yet in the array of all favorite ids, display "Add Button" -->         <a href="#" class="btn btn-primary btn-dark" ng-click="addToFavorites(wine.Id)" ng-if="favorites.indexOf(wine.Id) !> -1"> Add </a>         <!-- Else Display Already Added -->         <span ng-if="favorites.indexOf(wine.Id) > -1">Added</span>     </td> </tr> 

Here is my JS:

app.controller("MainController", function($scope, $http){     $scope.favorites = [];     var getAllFavorites = function(){         $http.get("/home/getAllFavoriteIds").success(function(response) {             angular.forEach(response, function(r) {                 $scope.favorites.push(r);             });         });     }; }); 

I am new to .indexOf() so I am thinking maybe that is the problem. But Maybe I am wrong.

like image 939
Wil Prim Avatar asked May 05 '15 17:05

Wil Prim


People also ask

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What is track by in NG-repeat?

Track by $index in AngularJS The ngRepeat track by $index allows you to specify which items should be repeated using their index number. The ngRepeat is a directive that can be added to the HTML template of an Angular application. It is used for creating lists, and it can take an expression as an argument.

What does ng-repeat do?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How do you use two ng-repeat in a table?

You can nest two ng-repeat together in order to create a more complex table. The first ng-repeat in the tr tag will create the rows and the second one in the td tag will create one column per element in the collection.


2 Answers

You can use angular-filter's contains filter:

<span ng-if="favorites | contains:wine.Id">Added</span> 

or write your own filter that does the same:

angular.module('module').filter('contains', function() {   return function (array, needle) {     return array.indexOf(needle) >= 0;   }; }); 
like image 150
fracz Avatar answered Sep 19 '22 14:09

fracz


I would recommend you to move this logic to controller an keep your view as clean as possible:

   $scope.isFavorites = function(id) {        return $scope.favorites.indexOf(id) !== -1;    } 

And your view should be:

<!-- If wine.Id is not yet in the array of all favorite ids, display "Add Button" --> <a href="#" class="btn btn-primary btn-dark" ng-click="addToFavorites(wine.Id)" ng-if="!isFavorites(wine.Id)">Add</a> <!-- Else Display Already Added --> <span ng-if="isFavorites(wine.Id)>Added</span> 
like image 44
Artem Petrosian Avatar answered Sep 23 '22 14:09

Artem Petrosian