是否可以将角度 n- 选项中的值连接起来

基本上,我尝试填充一个选择框,但是连接来自 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>
50375 次浏览

引号很重要,它不能在双引号中使用双引号,也不能在单引号中使用单引号。

什么管用:

<select ng-model="buyers" ng-options='b.id as (b.first_name + " " + b.last_name) for b in buyers'></select>

您可以使用以下语法连接字符串:

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>

注意,括号不是 需要,但它可以改进 可读性

试试这个

<select ng-model="buyers" ng-options="b as (b.first_name + " " + b.last_name) for b in buyers">
</select>