Issue
I have a JSON object which is as follows
[{
"id": 1,
"firstName": "Sam",
"middleName": "poller",
"lastName": "Aniston",
"address": "New York City",
}, {
"id": 2,
"firstName": "Apple",
"middleName": null,
"lastName": "Jolie",
"address": "Beverley Hills",
}, {
"id": 3,
"firstName": "Anna",
"middleName": "mary",
"lastName": "Dobrev",
"address": "London",
}]
I'm populating this data in view using select as,
<div >
<select ng-model="invigilator" ng-options="invigilator.id as (invigilator.firstName+' '+invigilator.middleName+' '+invigilator.lastName) for invigilator in invigilatorList" ng- click="getinvigilator(invigilator)" class="form-control">
<option value="">Select Invigilator</option>
</select></div>
But I am getting options as,
Sam poller Aniston
Apple null Jolie
Anna mary dobrev
how can i remove that null from the middle name and show only Apple Jolie.
Solution
Use the ternary operator:
<select ng-options="… as (invigilator.firstName + ' ' + (invigilator.middleName !== null ? (invigilator.middleName + ' ') : '') + invigilator.lastName) for …"></select>
Since the expression is becoming pretty complex, you can also (and you'd better to) move this code in a function:
<select ng-options="… as getFullName(invigilator) for …"></select>
// In the controller
$scope.getFullName = function (invigilator) {
if (invigilator.middleName === null) {
return invigilator.firstName + ' ' + invigilator.lastName;
}
return invigilator.firstName + ' ' + invigilator.middleName + ' ' + invigilator.lastName;
};
Answered By - Blackhole
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.