How do I set the value property in AngularJS' ng-options?

后端 未结 27 891
感动是毒
感动是毒 2020-11-22 08:17

Here is what seems to be bothering a lot of people (including me).

When using the ng-options directive in AngularJS to fill in the options for a &

27条回答
  •  遇见更好的自我
    2020-11-22 08:54

    Instead of using the new 'track by' feature you can simply do this with an array if you want the values to be the same as the text:

    
    

    Note the difference between the standard syntax, which will make the values the keys of the Object/Array, and therefore 0,1,2 etc. for an array:

    
    

    k as v becomes v as v.

    I discovered this just based on common sense looking at the syntax. (k,v) is the actual statement that splits the array/object into key value pairs.

    In the 'k as v' statement, k will be the value, and v will be the text option displayed to the user. I think 'track by' is messy and overkill.

提交回复
热议问题