简体   繁体   English

如何在angularjs中的花括号内添加条件?

[英]How to add condtion inside curly brace in angularjs?

I have variable called $scope.carname = "Volvo" and in html i need to check carname is exist or not if exist that time i need to show link otherwise i need to show some hard coded value( test ) 我有一个名为$scope.carname = "Volvo"变量,在html中,我需要检查carname是否存在,那时候我需要显示链接,否则我需要显示一些硬编码值( test

Below is my code 下面是我的代码

Html HTML

 <h1>{{carname ? <a href='link'>Go here </a> : test}}

controller 控制者

$scope.carname = "Volvo" ; $scope.carname = "Volvo" ;

$scope.link= "https://www.w3schools.com"

Here Is link 这里是链接

Use to use two ng-if expressions 用于使用两个ng-if表达式

<h1 ng-if="carname"> <a  href='link'>Go here </a>  </h1>
<h1 ng-if="!carname"> {{test}} </h1>

You need to use ng-if 您需要使用ng-if

 $scope.test ="123"
 $scope.carname = "Volvo";

HTML: HTML:

    <div ng-if="carname !== ''">{{test}}</div>
    <div ng-if="carname === ''">hard coded value</div>

plunker: http://next.plnkr.co/edit/0KBOPuiYsXmYNQfo 朋克: http ://next.plnkr.co/edit/0KBOPuiYsXmYNQfo

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

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