Questions tagged [angularjs-ng-if]

The AngularJS ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.

ngIf differs from ngShow and ngHide, as ngIf completely removes and recreates the element in the DOM rather than changing its visibility via the display css property. A common case when this difference is significant is when using css selectors that rely on an element's position within the DOM, such as the :first-child or :last-child pseudo-classes.

Note that when an element is removed using ngIf, its scope is destroyed and a new scope is created when the element is restored. The scope created within ngIf inherits from its parent scope using prototypal inheritance. An important implication of this is if ngModel is used within ngIf to bind to a javascript primitive defined in the parent scope. In this case any modifications made to the variable within the child scope will override (hide) the value in the parent scope.

Also, ngIf recreates elements using their compiled state. An example of this behavior is if an element's class attribute is directly modified after it's compiled, using something like jQuery's .addClass() method, and the element is later removed. When ngIf recreates the element the added class will be lost because the original compiled state is used to regenerate the element.

Additionally, you can provide animations via the ngAnimate module to animate the enter and leave effects.

ngIf docs

92 questions
1
vote
1 answer

AngularJS ng-if variable works only once

I am maintaining a code written some time ago, I have no experience in AngularJS. I have to turn on a spinner while an HTTP request is fired and to turn off it when the HTTP response is received. The spinner is controlled by a ng-if and the…
shogitai
  • 1,823
  • 1
  • 23
  • 50
1
vote
3 answers

Why do ng-if don't work inside a mat-dialog

I have created a mat-dialog component to fire for http response. When I include a ngIf statement inside the html for the mat-dialog,it is not handled. On the console it shows a warning as follows. Can't bind to 'ngIf' since it isn't a known property…
1
vote
1 answer

Ng-if: using plain expression instead of function call, does it make a difference?

I couldn't find an answer so I'm bringing it up here. Does it make a difference in resources or performance if I use $scope.isAllowed = true;
instead of $scope.isAllowed = function() { return true; };
Nebulosar
  • 1,727
  • 3
  • 20
  • 46
1
vote
1 answer

Browser freezes when use function api in ng-if AngularJS

I use angularjs and I have a problem with ng-if when I use a function that returns true or false in two API, the browser is freezes self.isShow= function() { service.getKioskLogByKioskId([stateParams.kioskId], function (data) { …
milad dn
  • 43
  • 1
  • 1
  • 9
1
vote
1 answer

Rendering partial views based on an ngif statement in pug (jade) and angularjs

Is it possible to have 2 separate .pug files be the result of an ng-if statement in a parent template? Something like ng-if({booleanVar ? pugFile1.pug : pugFile2.pug}) Or any way cleaner than having 2 full pages inside an ngif statement? Thanks…
Max
  • 123
  • 8
1
vote
1 answer

How to get nested ng-if triggering?

If I have a ng-if inside an ng-if, the inner ng-if won't initiate if the condition on the outer ng-if is in the process of hiding. I'd like the embedded ng-if to trigger even if the outer one is in the process of hiding. In this particular case I…
phazei
  • 5,323
  • 5
  • 42
  • 46
1
vote
2 answers

angularjs if else condition does not work

i am new in AngularJS and working on a small module in a project. I want to show two div based on condition. The condition is if {{ merchandiser.popCode }} means popcode value exist in database then show div1 else show div2. Here is my code…
1
vote
2 answers

Show a link based on a condition in angularJS

I'm creating a navigation bar in AngularJS. I am showing and hiding the submenu 'div.farm-links' on mouseover and mouseleave respectively on 'div.menu-links'. Now, in my submenu whenever 'child.thirdLevelChildList.length' is more than 5, it should…
Sunny
  • 902
  • 3
  • 18
  • 41
1
vote
2 answers

Using value of ng-model in ng-if

I have a text box where I enter an idNumber. I want to check if that id number appears in my table. If it does, I want to add a glyphicon to that row. Now, my problem is that I don't know how to access ng-model="idNumber" from ng-if. I try using…
chichi
  • 207
  • 1
  • 4
  • 13
1
vote
0 answers

Again: ng-click not working inside ng-repeat inside ng-if

This is most likely going to be another Angular scope question. I looked hard for a solution on SO, however, nothing I found would solve my problem. I am using Angular.js together with Swiper.js. Inside one slide of an outer vertical swiper, I have…
1
vote
1 answer

Error using array function in ng-if

This sounds very simple but I have been struggling with it! In AngularJs 1.4, I have an ng-if which is using array's some() function. I get syntax error Syntax Error: Token '>' not a primary expression at column ... when doing this. My HTML…
Vikram
  • 608
  • 1
  • 7
  • 18
1
vote
3 answers

can we have ng-show and ng-if in one div tag?

Following code snippet does not work Please suggest any other way of doing it `