AngularJS - value attribute for select

前端 未结 8 1921
时光取名叫无心
时光取名叫无心 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

    What you first tried should work, but the HTML is not what we would expect. I added an option to handle the initial "no item selected" case:

    
    
    selected: {{region}}

    The above generates this HTML:

    
    

    Fiddle

    Even though Angular uses numeric integers for the value, the model (i.e., $scope.region) will be set to AL, AK, or AS, as desired. (The numeric value is used by Angular to lookup the correct array entry when an option is selected from the list.)

    This may be confusing when first learning how Angular implements its "select" directive.

提交回复
热议问题