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
0
votes
1 answer

Not validating new controls after submitting form in angularjs

We have a form which we are submitting on save button. There are some validation firing on save. We are submitting form using $scope.isSubmitted= true and $scope.watch on $scope.isSubmitted for error highlighting After submitting and validating the…
Vikas Sardana
  • 1,593
  • 2
  • 18
  • 37
0
votes
1 answer

Can I use ng-click with ng-if to hide and show two different sections simultaneously

I'm working on an app in angularjs and trying to do something directly in the DOM. While I know this is not the greatest practice, it would be pretty practical in this case since I'm specifically trying to circumvent the controller. I tried to…
SealHead1
  • 29
  • 1
  • 1
  • 10
0
votes
1 answer

Angular 1/ ng-if one-time binding only in a condition

In my template i have :
In my component show() { if (angular.isDefined(this.parking.parkingType)) { return this.parking.parkingType.labelKey ===…
0
votes
1 answer

Angularjs 4 - Need to refresh for display/hide directive

I have a problem to hide the navbar when the user is not logged in (on the public views), I check if the item currentUser exists on the localStorage and then I use *ngIf on the html template to show/hide. When I login at first I don't see the…
Troyer
  • 6,765
  • 3
  • 34
  • 62
0
votes
1 answer

How to compare dates using ngIf in angular 2 and update the card background accordingly?

I have "md-grid-list" to display cards in the grid. I am looping my array of data using "ngFor". How to compare each of the datetime element in the array with the given DateTime using "ngIf"? and if the condition passes successfully, the color of…
0
votes
2 answers

ng-if not working with simple javascript

ng-if is not working when I change the values through simple javascript function.My function is getting called but the changes in values cannot be seen in view. Please refer below code. HTML
prateek tomar
  • 362
  • 1
  • 12
0
votes
1 answer

Directive under ng-if doesn't get recompiled when inserted in DOM

Here's an example which demonstrates the problem described in the title: https://plnkr.co/edit/Xn1qgYftc5YHMsrGj0sh?p=preview Directive code: .directive('translate', function($compile) { return { compile: function(element) { var html =…
0
votes
0 answers

Watch a function and all its variables to use it in ngIf

I want to apply a ngIf directive to a DOM element, this ngIf watch a function and that function check the values of other variables. As the following: export class Controller{ private x: ObjectClass; public isDone():boolean{ …
hex
  • 515
  • 2
  • 4
  • 13
0
votes
0 answers

display or hide loading from multiple controllers

I'm trying to create a generic loading that would be shared among controllers. So, I've created a service that will control the hide/show state: (function () { 'use strict'; var module = angular.module('main'); var serviceId =…
0
votes
0 answers

Ng-if doesn't work when contained in a String. How can I get it to work?

I have declared a variable called authenticated that I want to use to show or hide a button in a popover. The user should only see the button if he has already authenticated. I tried the ng-if with the variable in the h5 part and here it works. I…
0
votes
2 answers

Multiple ng-if on the same condition - performance wise

I have a html page with different element, that according to some condition (the same condition) will be shown/hidden using Angular's ng-if. In my controller, I create a boolean variable. $scope.myCondition = getValue(); In my view, I listen to…
Gil Epshtain
  • 8,670
  • 7
  • 63
  • 89
0
votes
1 answer

Count ng-if true statements in angularjs?

I have a page which shows different data with ng-ifs, I need to show how many of them are true on the same page. Just the number. The HTML is complex, but I have made a simpler version to understand how things work. Here is what the HTML looks like,…
Aijaz
  • 680
  • 15
  • 42
0
votes
2 answers

How to refactor this logic in the template/view to use a single line if statement (AngularJS 1.x)

I am fixing some small issues within our AngularJS application - I've come across the logic below which simply displays one link. In pseudocode style.. if (data.serviceId exists in the DOM) { display link & populate the href from data.serviceId…
Zabs
  • 13,852
  • 45
  • 173
  • 297
0
votes
3 answers

Angular ng-if not rendering div upon condition change

I'm working on learning Angular with Google maps and having difficulty rendering a map within a div controlled by an ng-if directive. For my application, I would like the map render upon receiving the coordinates from the $scope.location function. I…