Joshua Evensen Joshua Evensen - 3 months ago 7
Javascript Question

angular js - condition in select "comprehension expression"

i've got an angular app that maps one set of objects to another set of objects via two select elements.

when a mapping is made, the mapped objects' "mapped" property gets set to true.

<select ng-model='sourceToMap' required='true' ng-options='code.code for code in sourceCodes'>
<select ng-model='targetToMap' required='true' ng-options='code.code for code in targetCodes'></select>


i'd like my select to show all the items in sourceCodes/targetCodes whose mapped property is false. something like...

ng-options="code.code for code in sourceCodes where code.mapped = false"


if possible, i'd like to avoid having a mappedItems / unmappedItems collection cause that seems really gross

Answer

Use a filter, like this:

ng-options="code.code for code in sourceCodes | filter:{mapped: false}"

jsFiddle: http://jsfiddle.net/bmleite/T97DN/