angular.js - 焦虑!angular的ng-options的问题
迷茫
迷茫 2017-05-15 17:11:44
0
5
496

我现在需要绑定后台获取的数据到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里的字符类型,怎么破~~

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(5)
Peter_Zhu

ng-model="originOptions"

阿神

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

小葫芦

You can read this article

为情所困

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

左手右手慢动作

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...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template