使用angularjs数据类型的json下拉选项正在附加

drop down options from json using angularjs data type is appending

本文关键字:选项 angularjs 数据类型 json 使用      更新时间:2023-09-26

在AngularJS中用JSON填充select,带有ng个选项,

<select id="routeName-{{$index}}" ng-model="row.routeName" ng-options="routeName.id as routeName.name for routeName in routeNames" ng-change="flowRootChange($index)" style="width: 115px;"></select>

输入JSON如下:

scope.routeNames=[{"id":"1","name":"routeName1"},{"id":"2","name":"routeName2"}];

但在html中显示如下:

<select style="width: 115px;" ng-change="flowRootChange($index)" ng-options="routeName.id as routeName.name for routeName in routeNames" ng-model="row.routeName" id="routeName-0" class="ng-pristine ng-valid ng-touched">
   <option label="routeName1" value="string:1">routeName1</option>     
   <option label="routeName2" value="string:2">routeName2</option>
</select>

通过使用JQuery读取数据,我得到了string:2作为输出。

console.log($("#routeName-"+rowid +" :selected").val());

我不能使用ng-model/$scope.row.routeName,因为这个下拉列表在表中。如何在不使用split()方法的情况下读取所选值(没有string:)?。我使用的是"AngularJS v1.4.8"变体。

您可以使用ng repeat而不是ng选项

<select style="width: 115px;" ng-change="flowRootChange($index)" ng-model="row.routeName" >
   <option ng-repeat='routeName in routeNames' label="routeName{{ routeName.id  }}" value="{{ routeName.id }}">{{ routeName.name }}</option> 
</select>

检查这个:-

<select style="width: 115px;" ng-change="flowRootChange($index)" ng-model="name" >

{{routeName.name}}