Ad

How To Output Parameter Value Only When There Are The Specific Character?

- 1 answer

<td ng-click = 'membermodal(item)'>{{item.zipcode1}}</td>

-

<td ng-click = 'membermodal(item)'>{{item.zipcode2}}</td>

I want to be output zipCode1-zipCode2 when there is a zipCode1.

When there is not a zipCode1 it should not print -.

Ad

Answer

Use a simple conditional statement in place of the -:

{{ item.zipcode1 != "undefined" ? "-" : "" }}

See the following example for usage. Try commenting out the zipcode1 to see it in action.

var app = angular.module("App", []);
app.controller("Module", function() {
  this.zipcode1 = "try commenting this line out"
  this.zipcode2 = "hello";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="Module as item">
  {{ item.zipcode1 }}
  {{ item.zipcode1 != undefined ? "-" : "" }}
  {{ item.zipcode2 }}
</div>

Ad
source: stackoverflow.com
Ad