How to omit a specific ng-options item?

3

I have this code snippet below to load a comboBox with the 'name' of all values and is working correctly.

<select
    ng-model="information"
    ng-options="value.id as value.name for value in information">   
</select>

But I need to exclude the value.name whose id equals 301 from the load.   How do I load all value.name, except the value.name that has the id equal to 301?

    
asked by anonymous 06.08.2015 / 02:29

2 answers

5

You can apply a filter by object matching by denying this specific id, like this:

<select
    ng-model="information"
    ng-options="value.id as value.name for value in information | filter: {id: '!301'}">   
</select>

link

    
06.08.2015 / 03:19
2

In order not to leave the 'hit' code in ID: 301, I'll come up with a more elegant solution.

In the controller, you can add a business rule, and mark your object as checked, true or false, (false in case of your ID)

angular.forEach(information, function(value, index){
    //Adicionar regra de negócio aqui.
    if(regra)
       value.checked = true;
    else
       value.checked = false;
});

no html to use as well.

<select
    ng-model="information"
    ng-options="value.id as value.name for value in information | filter: {checked: true}">   
</select>

So you can block N records according to your business rule, so if you change from BD, your application will not perform such a treatment if the ID is another.

    
17.08.2015 / 21:50