removing null from angularjs select options


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=" as  (invigilator.firstName+' '+invigilator.middleName+' '+invigilator.lastName) for invigilator in invigilatorList"  ng- click="getinvigilator(invigilator)" class="form-control"> 
                                <option value="">Select Invigilator</option>


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.


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

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published