AngularJS - value attribute for select

前端 未结 8 1790
时光取名叫无心
时光取名叫无心 2020-12-02 09:32

Source JSON data is:

[
  {\"name\":\"Alabama\",\"code\":\"AL\"},
  {\"name\":\"Alaska\",\"code\":\"AK\"},
  {\"name\":\"American Samoa\",\"code\":\"AS\"},
           


        
相关标签:
8条回答
  • 2020-12-02 10:22

    You could modify you model to look like this:

    $scope.options = {
        "AL" : "Alabama",
        "AK" : "Alaska",
        "AS" : "American Samoa"
      };
    

    Then use

    <select ng-options="k as v for (k,v) in options"></select>
    
    0 讨论(0)
  • 2020-12-02 10:24

    you can use

    state.name for state in states track by state.code
    

    Where states in the JSON array, state is the variable name for each object in the array.

    Hope this helps

    0 讨论(0)
提交回复
热议问题