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
432
votes
12 answers

What is the difference between ng-if and ng-show/ng-hide

I'm trying to understand the difference between ng-if and ng-show/ng-hide, but they look the same to me. Is there a difference that I should keep in mind choosing to use one or the other?
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
71
votes
3 answers

Using ng-if as a switch inside ng-repeat?

I am working on Angular app. I tried to use ng-if and switch inside ng-repeat but didn't succeed. I have data like: **[{"_id":"52fb84fac6b93c152d8b4569", "post_id":"52fb84fac6b93c152d8b4567", "user_id":"52df9ab5c6b93c8e2a8b4567", …
Anil Sharma
  • 2,952
  • 5
  • 29
  • 45
60
votes
6 answers

AngularJS - How to use ng-if without HTML element

Is there a way to tell AngularJS not to display the top HTML element which has ng-if directive. I want angular to display child content only. Angular Code:
ABC
waqas
  • 4,357
  • 3
  • 34
  • 42
5
votes
4 answers

Why does ng-mouseover not work with ng-if

I'm trying to use "ng-mouseover" directive on an image that has "ng-if" and it doesn't work but If I use "ng-show" directive it works, can everyone tell me why? Or it's an AngularJS problem? In the AngularJS documentation, I can't read anything…
Aitor
  • 455
  • 7
  • 17
4
votes
1 answer

Why is ngif ignoring priority level?

I have created a custom directive with a priority of 1000. In the directive's compile function I am removing ng-if from the element. My assumption is that since ng-if has a lower priority of 600, it should not get compiled. app.js var app =…
4
votes
1 answer

How make ngIf directive globally visible in all project?

I have a code
Some data
And i have a error Can't bind to 'ngIf' since it isn't a known property of 'div'. How i can fix it?
John Doe
  • 3,794
  • 9
  • 40
  • 72
4
votes
3 answers

ng-if causes links to show for a second then disappear. How to prevent showing

ng-if sometimes causes links on my page to show during load time, and then disappear. The link is not supposed to show at all, and I am assuming that the ng-if statement hasn't been processed yet for the second I am able to see it. Is there a way to…
ASattar
  • 82
  • 1
  • 7
4
votes
1 answer

Different renderings using ng-if?

I have a JSON feed which provides a list of search results - The list contains products and regular pages. When rendering the list, the products has a different rendering than the regular pages, which means, different HTML if it is a product or a…
user431619
4
votes
2 answers

How to use ng-if with tr tag along with ng-repeat?

Can someone help me on this? Below is my code and the problem explanation. …
Karthik
  • 1,064
  • 2
  • 16
  • 33
3
votes
4 answers

Using ng-if to show certain things if condition is met

I'm trying to use ng-if to show certain rows if the condition is met, but can't quite seem to get it to work 100% correctly.

Covered:

Dave
  • 1,257
  • 2
  • 27
  • 58
3
votes
2 answers

how to Implement to check for null Date in angularJS using ng-if?

how to Implement to check for null Date in angularJS using ng-if ? The HTML Source Code is
B.Balamanigandan
  • 4,713
  • 11
  • 68
  • 130
2
votes
0 answers

In Angular2+ which one has the better performance? Using dynamic components or multiple *ngIf

I'm working on a data grid module. In the grid cells I need to display several types of data. So every cell is a component. Currently I'm using a ComponentFactoryResolver to generate the cells. Something like this:
Viktor
  • 93
  • 1
  • 6
2
votes
2 answers

Accessing DOM elements in directive after ng-if compiles

I am trying to build a side navigation menu, but want to dynamically populate its items based on the state of the DOM (i.e.
s in my DOM). However, I have some ng-ifs applied to various
s that are based on scope variables that are…
lebolo
  • 2,120
  • 4
  • 29
  • 44
2
votes
1 answer

Angular ng-if still run

Trying to figure out ng-if. Why is the alert function executed? Thanks
BlackMouse
  • 4,442
  • 6
  • 38
  • 65
1
vote
1 answer

directive with ng-if not rendering in Angular.js

I'm trying to render a directive whenever I get the search results back. My plan was to keep a scope variable algoliaSearch initially false and whenever I get back search results I change it to true. Then in my view I tried to render the directive…
cmgchess
  • 7,996
  • 37
  • 44
  • 62
1
2 3 4 5 6 7
Icon Name Details