Is it possible to concatenate values in Angular ng-options
You can concatenate a string using this synthax:
ng-options="i.x + ' ' + i.y for i in items"
The following snippet concatenate firstname
and 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>
Note that parenthesis are not required, but it may improve readability.
The quotes are important. It won't work with double quotes inside double quotes or single quotes inside single quotes.
What does work:
<select ng-model="buyers" ng-options='b.id as (b.first_name + " " + b.last_name) for b in buyers'></select>