是否可以在Angular ng-options中连接值
基本上,我试图填充select框,但连接来自first_name
列和last_name
列的值。
我想做什么(不起作用):
<select ng-model="buyers" ng-options="b.id as (b.first_name + " " + b.last_name) for b in buyers"></select>
什么工作:
<select ng-model="buyers" ng-options="b.id as b.first_name for b in buyers"></select>
引号是重要的。 它不会使用双引号内的双引号或单引号内的单引号。
什么工作:
<select ng-model="buyers" ng-options='b.id as (b.first_name + " " + b.last_name) for b in buyers'></select>
你可以使用这个合成器连接一个string:
ng-options="ix + ' ' + iy for i in items"
以下代码片断连接了firstname
和lastname
:
angular.module('myApp', []); function MyCtrl($scope) { $scope.names = [{firstname: 'Jon', lastname: 'Snow'}, {firstname: 'Rob', lastname: 'Stark'}, {firstname: 'Ned', lastname: 'Stark'}]; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <select ng-model="selected" ng-options="n.firstname + ' ' + n.lastname for n in names"></select> selected: {{selected}} </div>