Working with select using AngularJS's ng-options

前端 未结 8 855
渐次进展
渐次进展 2020-11-22 08:30

I have read about it in other posts, but I couldn\'t figure it out.

I have an array,

$scope.items = [
   {ID: \'000001\', Title: \'Chicago\'},
   {ID         


        
8条回答
  •  余生分开走
    2020-11-22 08:58

    For some reason AngularJS allows to get me confused. Their documentation is pretty horrible on this. More good examples of variations would be welcome.

    Anyway, I have a slight variation on Ben Lesh's answer.

    My data collections looks like this:

    items =
    [
       { key:"AD",value:"Andorra" }
    ,  { key:"AI",value:"Anguilla" }
    ,  { key:"AO",value:"Angola" }
     ...etc..
    ]
    

    Now

    
    

    still resulted in the options value to be the index (0, 1, 2, etc.).

    Adding Track By fixed it for me:

    
    

    I reckon it happens more often that you want to add an array of objects into an select list, so I am going to remember this one!

    Be aware that from AngularJS 1.4 you can't use ng-options any more, but you need to use ng-repeat on your option tag:

    
    

提交回复
热议问题