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

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

    Fact, that ng-if directive, unlike ng-show, creates its own scope, leads to interesting practical difference:

    angular.module('app', []).controller('ctrl', function($scope){
      $scope.delete = function(array, item){
        array.splice(array.indexOf(item), 1);
      }
    })
    
    
    

    ng-if:

    • {{show}}

    ng-show:

    • {{show}}

    ng-if with $parent:

    • {{show}}

    At first list, on-click event, show variable, from innner/own scope, is changed, but ng-if is watching on another variable from outer scope with same name, so solution not works. At case of ng-show we have the only one show variable, that is why it works. To fix first attempt, we should reference to show from parent/outer scope via $parent.show.

提交回复
热议问题