Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS filtering - multiple expressions or dynamically chain ng-filters?
    primarykey
    data
    text
    <p>If you take a look at the code below, I'd like to use a text <code>&lt;input&gt;</code> to filter each menu item by multiple ingredients - for example, if the user typed "beef, bacon" into the <code>&lt;input&gt;</code>, the app would return ALL menu items with beef OR bacon as an ingredient.</p> <p>I'm currently trying to do this using ng-filter, and I'm guessing I need to create a custom filter in order to this. Is this the wrong approach?? Is there a way to dynamically chain filters instead?</p> <p>Here's some code which should make sense of my problem -</p> <p><strong>My search model:</strong> <em>- Note: using ng-list to turn string into an array of substrings</em></p> <pre><code>&lt;div ng-init="searchString=[]"&gt; &lt;input type="text" ng-model="searchString" ng-list&gt; &lt;/div&gt; </code></pre> <p><strong>My ng-repeat loop:</strong> <em>- Note: using a custom filter to join each of my ingredient into one string</em></p> <pre><code>&lt;tr ng-repeat="item in menu | filter:{ category : 'Classics' } | filter:{ ingredients : searchString } "&gt; &lt;td class="title"&gt;{{ item.title }}&lt;/td&gt; &lt;td class="ingredients"&gt; {{ item.ingredients | join:', ' }} &lt;/td&gt; &lt;td class="price"&gt;{{ item.price | currency }}&lt;/td&gt; &lt;/tr&gt; </code></pre> <p><strong>My data structure</strong></p> <pre><code>$scope.menu = [ { "title" : "New Yorker", "price" : "4.00", "ingredients" : [ "Salt Beef", "Pickles", "Mustard" ], "category" : "Classics" }, { "title" : "BLT", "price" : "4.00", "ingredients" : [ "Bacon", "Lettuce", "Tomato" ], "category" : "Classics" } ] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload