74

基本上,我试图填充一个选择框,但连接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>
4

3 回答 3

137

引号很重要。它不适用于双引号内的双引号或单引号内的单引号。

有什么作用:

<select ng-model="buyers" ng-options='b.id as (b.first_name + " " + b.last_name) for b in buyers'></select>
于 2014-06-07T15:29:52.860 回答
4

您可以使用此合成器连接字符串:

ng-options="i.x + ' ' + i.y for i in items"

以下代码段连接firstnamelastname

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>

请注意,括号不是必需的,但它可以提高可读性

于 2017-04-24T13:31:16.223 回答
0

试试这个

<select ng-model="buyers" ng-options="b as (b.first_name + " " + b.last_name) for b in buyers">
</select>
于 2021-02-16T08:47:33.223 回答