简体   繁体   English

HTML标记上的Angular.js ternery语句

[英]Angular.js ternery statement on HTML tag

 <h4 class="changecolor">{{driver.name}}&nbsp;</h4>
<h4 class="changecolor" ng-show="!driver.name">{{driver.officeEmail}}&nbsp;</h4>

If the driver.name is not available then show the driver.officeEmail 如果driver.name不可用,则显示driver.officeEmail

Is there a way to implement this in one line, using ternary statement? 有没有办法在一行中使用三元语句实现它?

If you only want to use one binding, try: 如果您只想使用一个绑定,请尝试:

<h4 class="changecolor">{{driver.name || driver.officeEmail}}</h4>

This is called short-circuit evaluation . 这称为短路评估 If driver.name is undefined, then driver.officeEmail will be shown instead. 如果driver.name未定义,然后driver.officeEmail将被代替。

The answer by @quantumwannabe works and is a better solution for this particular use case, but the OP asked about ternary which is also possible since version 1.2.x. @quantumwannabe的答案有效,并且是针对此特定用例的更好解决方案,但OP询问有关三元数的问题,从1.2.x版本开始,这也是可能的。

 angular.module("app", []) .controller('appCtrl', ['$scope', function($scope) { $scope.driver = {email : "test"}; } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app"> <div ng-controller="appCtrl"> <h4 class="changecolor">{{driver.name ? driver.name : driver.email}}</h4> </div> </div> 

<h4 class="changecolor">{{(driver.name) ? driver.name : driver.officeEmail}}&nbsp;</h4>

这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM