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

后端 未结 27 826
感动是毒
感动是毒 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 09:00

    The correct answer to this question has been provided by user frm.adiputra, as currently this seems to be the only way to explicitly control the value attribute of the option elements.

    However, I just wanted to emphasize that "select" is not a keyword in this context, but it is just a placeholder for an expression. Please refer to the following list, for the definition of the "select" expression as well as other expressions that can be used in ng-options directive.

    The use of select as it is depicted in the question:

    ng-options='select p.text for p  in resultOptions'
    

    is essentially wrong.

    Based on the list of expressions, it seems that trackexpr may be used to specify the value, when options are given in an array of objects, but it has been used with grouping only.


    From AngularJS' documentation for ng-options:

    • array / object: an expression which evaluates to an array / object to iterate over.
    • value: local variable which will refer to each item in the array or each property value of object during iteration.
    • key: local variable which will refer to a property name in object during iteration.
    • label: The result of this expression will be the label for element. The expression will most likely refer to the value variable (e.g. value.propertyName).
    • select: The result of this expression will be bound to the model of the parent element. If not specified, select expression will default to value.
    • group: The result of this expression will be used to group options using the DOM element.
    • trackexpr: Used when working with an array of objects. The result of this expression will be used to identify the objects in the array. The trackexpr will most likely refer to the value variable (e.g. value.propertyName).

提交回复
热议问题