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

前端 未结 12 1385
执笔经年
执笔经年 2020-11-22 02:22

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 differenc

相关标签:
12条回答
  • 2020-11-22 03:10

    ng-show and ng-hide work in opposite way. But the difference between ng-hide or ng-show with ng-if is,if we use ng-if then element will created in the dom but with ng-hide/ng-show element will be hidden completely.

    ng-show=true/ng-hide=false:
    Element will be displayed
    
    ng-show=false/ng-hide=true:
    element will be hidden
    
    ng-if =true
    element will be created
    
    ng-if= false
    element will be created in the dom. 
    
    0 讨论(0)
  • 2020-11-22 03:12

    To note, a thing that happened to me now: ng-show does hide the content via css, yes, but it resulted in strange glitches in div's supposed to be buttons.

    I had a card with two buttons on the bottom and depending on the actual state one is exchanged with an third, example edit button with new entry. Using ng-show=false to hide the left one(present first in the file) it happened that the following button ended up with the right border outside of the card. ng-if fixes that by not including the code at all. (Just checked here if there are some hidden surprises using ng-if instead of ng-show)

    0 讨论(0)
  • 2020-11-22 03:15

    @EdSpencer is correct. If you have a lot of elements and you use ng-if to only instantiate the relevant ones, you are saving resources. @CodeHater is also somewhat correct, if you are going to remove and show an element very often, hiding it instead of removing it could improve performance.

    The main use case I find for ng-if is that it allows me to cleanly validate and eliminte an element if the contents is illegal. For instance I could reference to a null image name variable and that will throw an error but if I ng-if and check if it's null, it's all good. If I did an ng-show, the error would still fire.

    0 讨论(0)
  • 2020-11-22 03:18
    1. ng-if if false will remove elements from DOM. This means that all your events, directives attached to those elements will be lost. For example, ng-click to one of child elements, when ng-if evaluates to false, that element will be removed from DOM and again when it is true it is recreated.

    2. ng-show/ng-hide does not remove the elements from DOM. It uses CSS styles (.ng-hide) to hide/show elements .This way your events, directives that were attached to children will not be lost.

    3. ng-if creates a child scope while ng-show/ng-hide does not.

    0 讨论(0)
  • 2020-11-22 03:19

    ngIf

    The 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.

    <!-- when $scope.myValue is truthy (element is restored) -->
    <div ng-if="1"></div>
    
    <!-- when $scope.myValue is falsy (element is removed) -->
    <div ng-if="0"></div>
    

    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.

    If ngModel is used within ngIf to bind to a JavaScript primitive defined in the parent scope, any modifications made to the variable within the child scope will not affect the value in the parent scope, e.g.

    <input type="text" ng-model="data">
    <div ng-if="true">
        <input type="text" ng-model="data">
    </div>        
    

    To get around this situation and update the model in the parent scope from inside the child scope, use an object:

    <input type="text" ng-model="data.input">
    <div ng-if="true">
        <input type="text" ng-model="data.input">
    </div>
    

    Or, $parent variable to reference the parent scope object:

    <input type="text" ng-model="data">
    <div ng-if="true">
        <input type="text" ng-model="$parent.data">
    </div>
    

    ngShow

    The ngShow directive shows or hides the given HTML element based on the expression provided to the ngShow attribute. The element is shown or hidden by removing or adding the ng-hide CSS class onto the element. The .ng-hide CSS class is predefined in AngularJS and sets the display style to none (using an !important flag).

    <!-- when $scope.myValue is truthy (element is visible) -->
    <div ng-show="1"></div>
    
    <!-- when $scope.myValue is falsy (element is hidden) -->
    <div ng-show="0" class="ng-hide"></div>
    

    When the ngShow expression evaluates to false then the ng-hide CSS class is added to the class attribute on the element causing it to become hidden. When true, the ng-hide CSS class is removed from the element causing the element not to appear hidden.

    0 讨论(0)
  • 2020-11-22 03:22

    The ng-if directive removes the content from the page and ng-show/ng-hide uses the CSS display property to hide content.

    This is useful in case you want to use :first-child and :last-child pseudo selectors to style.

    0 讨论(0)
提交回复
热议问题