search

Home  >  Q&A  >  body text

angular.js - 焦虑!angular的ng-options的问题

我现在需要绑定后台获取的数据到select上

后台返回的JSON:

js部分:

<select ng-model="originOptions.nation_id" ng-options="x.nation_id as x.name for x in nation">
    <option value="">请选择</option>
</select>
    

然后,读取用户的选择数据,把用户的数据显示到select上,选中状态

但是怎么都绑定不了,一看控制台:

value上全部出现了number:1这样的格式,OK!那我加上track by x.nation_id

number是没有了,但是数据又绑定不了了!

比如:用户选择的是苗族,那么nation_id是6,按道理

$scope.originOptions = {'nation_id':6}

可以绑定上啊!但是就是不行!是我哪里出错了吗

请问大家怎么回事啊!!

我刚又实验了下:加了track by就绑定不上,去掉就可以!
但是又不要value里的字符类型,怎么破~~

迷茫迷茫2744 days ago500

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-05-15 17:13:44

    ng-model="originOptions"

    reply
    0
  • 阿神

    阿神2017-05-15 17:13:44

    No problem~ That’s it. What you get in the controller is the id value$scope.originOptions.nation_id

    reply
    0
  • 高洛峰

    高洛峰2017-05-15 17:13:44

    You can read this article

    reply
    0
  • 为情所困

    为情所困2017-05-15 17:13:44

    controller:

    $scope.data = {
        model: null,
        nation: [
          {nation_id: '1', name: 'A'},
          .......
          .......
        ]
       };

    HTML:

    <select name="mySelect" id="mySelect"
          ng-options="x.nation_id as x.name for x in data.nation"
          ng-model="data.model"></select>

    Try this data structure

    reply
    0
  • ringa_lee

    ringa_lee2017-05-15 17:13:44

    The character type in value cannot be obtained through the bound ng-model.

    Although it shows number:1

    ngOptions is generated like this

    You can take a look at the documentation for examples:
    https://docs.angularjs.org/ap...

    reply
    0
  • Cancelreply