简体   繁体   English

如何在angularJS中使用ng-if,ng-else

[英]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> 

Use ng-switch with expression and ng-switch-when for matching expression value: 在表达式中使用ng-switchng-switch-when匹配表达式值ng-switch-when使用ng- ng-switch-when

<div ng-switch="data.id">
  <div ng-switch-when="5">...</div>
  <div ng-switch-default>...</div>
</div>

Example is here 例子在这里

Angular2 example Angular2示例

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指令的重要问题:

  1. Directives will respond the same place where it's execute. 指令将在其执行的同一位置响应。
  2. ng-else concept is not there, you can with only if , or other flavor like switch statement. NG-其他概念是不存在的,你可以仅仅是如果 ,或者其他味道像switch语句。

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.

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