2

I'm trying to use a ng-options with a filter and the behavior seen is expected. However, the expected behavior is not what is needed.

What would be the best practice when doing something like:

<div ng-init="blades = [{item:'1', spec:'2'}]"></div>
<select 
    ng-model="blades"
    ng-options="blade.spec as blade.spec for blade in blades">
    <option>--</option>
</select>
<ul>
    <li ng-repeat="blade in blades">{{blades.item}}</li>
</ul>

Here, when you filter, you filter the values out of the select list rendering it useless. What's the best practice?

2 Answers 2

5

I answered my own question and a bunch of others in the process. This is how :

<div ng-init="items = [{id:'1', spec:'A'}, {id:'2', spec:'B']"></div>
<select 
  ng-model="searchSpec"
  ng-options="item.spec as item.spec for item in items">
  <option>--</option>
</select>
<ul>
  <li ng-repeat="item in items | filter:{spec:searchSpec}">{{item.spec}}</li>
</ul>
Sign up to request clarification or add additional context in comments.

Comments

0

Angular has a built-in way for doing this. Make ng-model="search.spec" and search will always be {spec:(selectedValue)}. It's nice, because you can tie multiple select dropdowns to different keys of the search object.

<div ng-init="items = [{id:'1', spec:'A'}, {id:'2', spec:'B']"></div>
<select 
  ng-model="search.spec"
  ng-options="item.spec as item.spec for item in items">
    <option>--</option>
</select>
<ul>
    <li ng-repeat="item in items | filter:search">{{item.spec}}</li>
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.