suchen

Heim  >  Fragen und Antworten  >  Hauptteil

angular.js - 关于 angularjs 的 ng-options 指令

我通过 ajax 在控制器中拉取到一个数组,var fruits = ['apple', 'banana'],希望实现的是如下效果:

<select>
  <option value='apple'>苹果</option>
  <option value='banana'>香蕉</option>
</select>
<select ng-options="fruit for fruit in fruits">
</select>

得到了

<select ng-options="fruit for fruit in fruits">
  <option value="0" selected="selected" label="apple">apple</option>
  <option value="1" selected="selected" label="apple">banana</option>
</select>

应该怎么通过修改 ng-options 标签来修改 value 的值呢?

PHPzPHPz2745 Tage vor565

Antworte allen(3)Ich werde antworten

  • 伊谢尔伦

    伊谢尔伦2017-05-15 17:01:55

    干脆直接ng-repeat option 吧···

    其实也是有解决办法的:
    http://stackoverflow.com/questions/12139152/how-to-set-the-value-property-in-angularjs-ng-options

    Antwort
    0
  • 大家讲道理

    大家讲道理2017-05-15 17:01:55

    fruit改成对象数组的方式

    Antwort
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-15 17:01:55

    var data = {"201":"活动1","202":"活动2","203":"活动3"}  
    
    <select class="form-control" ng-model="activityId" ng-options="id as name for (id, name) in data" ></select>
    
    var toys = [{"id":"11", "name":"玩具1"},{"id":"12", "name":"玩具2"},{"id":"13", "name":"玩具3"}];
    
    <select class="form-control" ng-model="toyId" ng-options="toy.id as toy.title for toy in toys" required></select>

    Antwort
    0
  • StornierenAntwort