Angular5 *ngIf 和 hidden 的区别

问题

项目中遇到一个问题,有一个过滤查询的面板,需要通过一个展开折叠的button,来控制它的show 和 hide。这个面板中,有一个Select 组件,一个 input 查询输入框。

725304-20181204101828506-174840323.png

原来代码是:

<div class="accordion-content" *ngIf="showFilter">
     <div class="pTop">
         <div app-choosen-select [options]="selectCompOptions" (selectedDataChange)="onFilterChange($event)" class="cpu-select"></div>
     </div>

     <div class="form-group">
         <input type="text" placeholder="查询" [(ngModel)]="searchTxt" (ngModelChange)="searchNode()">
     </div>
</div>

然后发现,每次点击 toggle button,都会触发一次onFilterChange()事件,因为 app-choosen-select 组件在初始化时会触发传入的 selectedDataChange 事件。从而影响到预期的结果。

解决方案

将 `*ngIf` 改成 `hidden`。
<div class="accordion-content" [hidden]="!showFilter">
   <div class="pTop">
       <div app-choosen-select [options]="selectCompOptions" (selectedDataChange)="onFilterChange($event)" class="cpu-select"></div>
   </div>

   <div class="form-group">
        <input type="text" placeholder="查询" [(ngModel)]="searchTxt" (ngModelChange)="searchNode()">
    </div>
</div>

What is the difference between *ngIf and [hidden]?

参考:https://stackoverflow.com/questions/43034758/what-is-the-difference-between-ngif-and-hidden

ngIf is a structural directive, it creates/destroys content inside the DOM. The [hidden] statement just hides/shows the content with css, i.e. adding/removing display:none to the element's style.

也就是,*ngIf=true 时,会重新创建其内部的 DOM 元素包括子组件,同时如果设置了数据绑定、事件绑定,也会重新绑定;*ngIf=false 时,会销毁其内部的DOM 元素和子组件,也会销毁绑定的数据和绑定的事件。

但是,[hidden]=true,只是隐藏了DOM元素和子组件,并没有重新初始化,绑定的数据和事件都还在的。

So [hidden] is better used when we want the show/hide status to change frequently, for example on a button click event, so we do not have to load the data every time the button is clicked, just changing its hidden attribute would be enough.
Note that the performance difference may not be visible with small data, only with larger objects.

所以,在这样的toggle 某一个元素的情况下,使用[hidden] 比较好。

转载于:https://www.cnblogs.com/liulei-cherry/p/10062680.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值