[英]How to make use of ng-if , ng-else in angularJS
I want to compare id.here if id equals 5 do this, else do that. 我想比较id。如果id等于5则执行此操作,否则执行此操作。 How can I achieve this? 我该如何实现?
<div class="case" data-ng-if="data.id === '5' ">
<input type="checkbox" id="{{data.id}}" value="{{data.displayName}}"
data-ng-model="customizationCntrl.check[data.id1]"
data-ng-checked="{{data.status}}=='1'" onclick="return false;">{{data.displayName}}
<br>
</div>
<div class="case" data-ng-else>
<input type="checkbox" id="{{data.id}}" value="{{data.displayName}}"
data-ng-model="customizationCntrl.check[data.id]"
data-ng-checked="{{data.status}}=='1'">{{data.displayName}}<br>
</div>
There is no directive for ng-else ng-else没有指令
You can use ng-if to achieve if(){..} else{..} in angularJs. 您可以使用ng-if在angularJs中实现if(){..} else {..} 。
For your current situation, 就您目前的情况而言,
<div ng-if="data.id == 5">
<!-- If block -->
</div>
<div ng-if="data.id != 5">
<!-- Your Else Block -->
</div>
You can also try ternary operator. 您也可以尝试使用三元运算符。 Something like this 像这样
{{data.id === 5 ? "it's true" : "it's false"}}
Change your html code little bit and try this hope so it will be work for you. 稍微更改您的html代码,然后尝试执行此希望,这样对您来说就会有用。
The syntax for ng if else in angular is : ng的语法,如果不是,则为:
<div class="case" *ngIf="data.id === '5'; else elsepart; ">
<input type="checkbox" id="{{data.id}}" value="{{data.displayName}}"
data-ng-model="customizationCntrl.check[data.id1]" data-ng-checked="
{{data.status}}=='1'" onclick="return false;">{{data.displayName}}<br>
</div>
<ng-template #elsepart>
<div class="case">
<input type="checkbox" id="{{data.id}}" value={{data.displayName}}"
data-ng-model="customizationCntrl.check[data.id]" data-ng-checked="
{{data.status}}=='1'">{{data.displayName}}<br>
</div>
</ng-template>
I am adding some of the important concern about ng directives :- 我添加了一些有关ng指令的重要问题:
Check out the below example:- 看看下面的例子:
<div ng-if="data.type == 'FirstValue' "> <div ng-if =“ data.type =='FirstValue'”>
//different template with hoot data
</div> </ div>
<div ng-if="data.type == 'SecondValue' "> <div ng-if =“ data.type =='SecondValue'”>
//different template with story data
</div> </ div>
<div ng-if="data.type == 'ThirdValue' "> <div ng-if =“ data.type =='ThirdValue'”>
//different template with article data
</div> </ div>
As per datatype it is going to render any one of the div. 根据数据类型,它将呈现div中的任何一个。
<span ng-if="verifyName.indicator == 1"><i class="fa fa-check"></i></span>
<span ng-if="verifyName.indicator == 0"><i class="fa fa-times"></i></span>
try this code. 试试这个代码。 here verifyName.indicator value is coming from controller. 这里的verifyName.indicator值来自控制器。 this works for me. 这对我有用。
You can write as: 您可以这样写:
<div class="case" ng-if="mydata.id === '5' ">
<p> this will execute </p>
</div>
<div class="case" ng-if="mydata.id !== '5' ">
<p> this will execute </p>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.