How to omit a specific ng-options item?

Asked

Viewed 314 times

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>

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

  • 1

    I’m not sure, but I think I’d have to build a filter and apply it to the variable information.

2 answers

5

2

To not leave the code 'failed' in the ID: 301, step a more elegant solution.

in the controller, you can add a business rule, and mark your object as checked, to true or false, (false in the 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;
});

in html use like this.

<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, by changing BD, your application will not perform such treatment if the ID is another.

  • 1

    I mean, fail 301 on the controller, that’s it?

  • yes will work, but I would not fail 301, because if you change the database, certainly will not be more this ID, IE, will not work, I would add the RULE, which does not apply to 301

  • I understand, but the way he posed the question maybe there’s no rule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.