Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make use of ng-if , ng-else in angularJS

Tags:

angularjs

I want to compare id.here if id equals 5 do this, else do that. 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>  
like image 852
satyendra kumar Avatar asked Sep 24 '15 08:09

satyendra kumar


People also ask

Can you use Ng-if and Ng show together?

ng-if is better in this regard. Using it in place of ng-show will prevent the heavy content from being rendered in the first place if the expression is false. However, its strength is also its weakness, because if the user hides the chart and then shows it again, the content is rendered from scratch each time.

What is Ng-IF and ELSE condition?

ngIf is a structural directive, which means that you can add it to any element like div , p , h1 , component selector, etc. Like all structural directive, it is prefixed with * asterisk. We bind the *ngIf to an expression (a condition in the above example). The expression is then evaluated by the ngIf directive.

What is * ngIf Angular?

A shorthand form of the directive, *ngIf="condition" , is generally used, provided as an attribute of the anchor element for the inserted template. Angular expands this into a more explicit version, in which the anchor element is contained in an <ng-template> element.


1 Answers

Use ng-switch with expression and ng-switch-when for matching expression value:

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

Example is here

Angular2 example

like image 127
Vasyl Avatar answered Oct 07 '22 08:10

Vasyl