Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-if="" with multiple arguments

I am trying to get started on angular development. And after reviewing the documentation some questions persist. How do i best write a ng-if with multiple arguments corresponding to

if( a && b) or if( a || b )

like image 218
David Karlsson Avatar asked Sep 24 '13 17:09

David Karlsson


People also ask

Can we use two conditions in ngIf?

We can use multiple conditions in *ngIf with logical operator AND (&&) to decide the trustworthy of *ngIf expression. If all conditions are true, then element will be added to the DOM.

Can we use ngIf in Div?

On the div tag we added the ngIf directive that would be displayed if the value of toggleOn is false. After that, we added some dummy paragraphs. This is how to use the ngIf directive. It can be used in all types of use cases and comparisons you can think of in your component template.


2 Answers

It is possible.

<span ng-if="checked && checked2">   I'm removed when the checkbox is unchecked. </span> 

http://plnkr.co/edit/UKNoaaJX5KG3J7AswhLV?p=preview

like image 72
javaCity Avatar answered Sep 24 '22 12:09

javaCity


Just to clarify, be aware bracket placement is important!

These can be added to any HTML tags... span, div, table, p, tr, td etc.

AngularJS

ng-if="check1 && !check2" -- AND NOT ng-if="check1 || check2" -- OR ng-if="(check1 || check2) && check3" -- AND/OR - Make sure to use brackets 

Angular2+

*ngIf="check1 && !check2" -- AND NOT *ngIf="check1 || check2" -- OR *ngIf="(check1 || check2) && check3" -- AND/OR - Make sure to use brackets 

It's best practice not to do calculations directly within ngIfs, so assign the variables within your component, and do any logic there.

boolean check1 = Your conditional check here... ... 
like image 34
Chris Avatar answered Sep 22 '22 12:09

Chris