Search code examples
angularjsangularjs-ng-repeat

Can I hide a null part of an AngularJS expression?


I have an object that has 3 fields:

obj.firstName
obj.middleName
obj.lastName

I have an array (objArray) of these objects that I am showing in an ng-repeat like this:

<tr ng-repeat="obj in objArray">
    <td>{{ obj.lastName + ", " + obj.firstName + " " + obj.middleName }}</td>
</tr>

The problem is that I have some obj's that have null as a value for obj.middleName. Is there a way to not display null if part of the expression happens to be null? I see that you can use a filter to remove one element completely from the ng-repeat, but I still want to display an element that has firstName and lastName. I just want it so that it doesn't display like this:

Smith, John null

But rather just displays this:

Smith, John


Solution

  • You can use join

     <td>{{ [obj.lastName + ',' ,obj.firstName, obj.middleName].join(' ') }}</td>
    

    JSFiddle Demo: http://jsfiddle.net/HB7LU/21495/