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

angularjs - ngswitch with 4 conditions

I need to write an angular ng-switch with 4 conditions. Some other answers here on SO suggest using ng-if instead of ng-switch. And so I tried with the ng-if, but I don't really think this is the best way and I'll explain why: Basically I need to…
Nick
  • 13,493
  • 8
  • 51
  • 98
0
votes
1 answer

Angular ng-if clause not working both ways

I am displaying a json in a table, like so: Where allRooms contains json objects like this: { "Name": "Room five", "OpenTime": "2016-02-19 00:00:00", "Entries": 2, "Capacity": 10, …
bdh
  • 127
  • 1
  • 2
  • 8
0
votes
0 answers

What's the right way to conditionally display forms in AngularJS

I am working on a little project that supports search filters. I have a default select dropdown from which a user can select e.g. Product Name, Status and Date. These options determine which type of form to display next or replace. When the user…
0
votes
1 answer

ng-if inside ng-repeat always returning true

I am creating a table with data from the Premier League. I am trying to use ng-if to check if the home team won and if so add a cell saying "Home team won" HTML
Home Team Won!
However…
JaAnTr
  • 896
  • 3
  • 16
  • 28
0
votes
1 answer

Can ng-repeat take user defined $index increment in Angular Js?

0
votes
2 answers

ng-if not working with ng-repeat

This should be a simple issue, but I don't understand what's happening. Basically I'm passing an object to a function call in my controller with ng-if withing an ng-repeat. However my function doesn't see the param passed to it. It says undefined.…
0
votes
0 answers

I don't understand how ng-if work in a list

I don't understand how ng-if works: I would like that a button in a list is visible only if a condition is verified. In the example below, a button should only be visible if a list element has title == 'Title2'
0
votes
2 answers

Test if a value exists in a map in a ng-if

How can I test if a value exists in a map in a ng-if? $scope.textInputTypes = { "currency": true, "double": true, "percent": true, "int": true, "email": true, "phone": true, "string": true, "textarea":…
angelokh
  • 9,426
  • 9
  • 69
  • 139
0
votes
1 answer

AngularJS: ngIf and ngRepeat

I am using ngRepeat and ngIf to filter/sort(?) some data. I originally considered using ngSwitch instead of ngIf for this, but couldn't find a good example. After fumbling around with the slightly more complex ngSwitch, I decided to give it a go…
UltraSonja
  • 881
  • 1
  • 20
  • 33
0
votes
1 answer

How to put $last ngRepeat result out of href?

Is there any option to put $last ng-repeat result like this
  • Four
  • instead of
  • Four
  • ? I have been trying with ng-if but unfortunately it did not give good solution yet :( DEMO:…
    Mo.
    • 26,306
    • 36
    • 159
    • 225
    0
    votes
    1 answer

    Why ng-if is not working inside ng-repeat

    I cannot properly use my ng-if inside an ng-repeat. I've updated my AngularJS to the newest version (09/27/2014) and I still can not get it to work right. I have code that works fine outside of the ng-repeat and I also have code that works inside…
    user1789573
    • 515
    • 2
    • 8
    • 23
    -1
    votes
    1 answer

    ngIf doesn't update with variable

    I have a very simple ng-if construction using two MD buttons and one AngularJS scope variable isPlaying. However, the ng-if doesn't seem to work along with the variable. I can see the variable changing as I click the buttons, using console tools,…
    -2
    votes
    1 answer

    Angular change template element based on the radio button

    I have two radio buttons in my template. on selecting a button, different input text fields should be displayed for each button and the other fields should hide. How can I do this using Angular? I have tried using *ngIf and ngModel, but not able to…