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

后端 未结 27 1350
感动是毒
感动是毒 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:05

    I had this issue too. I wasn't able to set my value in ng-options. Every option that was generated was set with 0, 1, ..., n.

    To make it right, I did something like this in my ng-options:

    HTML:

    
    

    I use "track by" to set all my values with room.price.

    (This example sucks: because if there were more than one price equal, the code would fail. So BE SURE to have different values.)

    JSON:

    $scope.Rooms = [
                { name: 'SALA01', price: 100 },
                { name: 'SALA02', price: 200 },
                { name: 'SALA03', price: 300 }
            ];
    

    I learned it from blog post How to set the initial selected value of a select element using Angular.JS ng-options & track by.

    Watch the video. It's a nice class :)

提交回复
热议问题