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

前端 未结 12 1404
执笔经年
执笔经年 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:01

    @Gajus Kuizinas and @CodeHater are correct. Here i am just giving an example. While we are working with ng-if, if the assigned value is false then the whole html elements will be removed from DOM. and if assigned value is true, then the html elements will be visible on the DOM. And the scope will be different compared to the parent scope. But in case of ng-show, it wil just show and hide the elements based on the assigned value. But it always stays in the DOM. Only the visibility changes as per the assigned value.

    http://plnkr.co/edit/3G0V9ivUzzc8kpLb1OQn?p=preview

    Hope this example will help you in understanding the scopes. Try giving false values to ng-show and ng-if and check the DOM in console. Try entering the values in the input boxes and observe the difference.

    
    

    Hello Plunker!

    
    

    ng-show=true ::

    ng-if=true ::
    {{data}}

提交回复
热议问题