container view_Angular视图容器删除view实例的过程分析

应用程序调用viewContainer的clear方法清除里面包含的视图实例:

50f11047471cdbf378fbcd59c0cc3ff8.png

clear的实现逻辑是,在一个循环里删除尾部的视图实例直至container为空:

6f6a960f4205f084353324d8ae738bb4.png

从detachView的注释能发现,不仅view实例从view container中移除,并且对应的DOM元素也从HTML页面中删除了:

/** * Detaches a view from a container. * * This method removes the view from the container's array of active views. It also * removes the view's elements from the DOM. * * @param {?} lContainer The container from which to detach a view * @param {?} removeIndex The index of the view to detach * @return {?} Detached LView instance. */function detachView(lContainer, removeIndex) {    if (lContainer.length <= CONTAINER_HEADER_OFFSET)        return;    /** @type {?} */    const indexInContainer = CONTAINER_HEADER_OFFSET + removeIndex;    /** @type {?} */    const viewToDetach = lContainer[indexInContainer];    if (viewToDetach) {        /** @type {?} */        const declarationLContainer = viewToDetach[DECLARATION_LCONTAINER];        if (declarationLContainer !== null && declarationLContainer !== lContainer) {            detachMovedView(declarationLContainer, viewToDetach);        }        if (removeIndex > 0) {            lContainer[indexInContainer - 1][NEXT] = (/** @type {?} */ (viewToDetach[NEXT]));        }        /** @type {?} */        const removedLView = removeFromArray(lContainer, CONTAINER_HEADER_OFFSET + removeIndex);        addRemoveViewFromContainer(viewToDetach[TVIEW], viewToDetach, false, null);        // notify query that a view has been removed        /** @type {?} */        const lQueries = removedLView[QUERIES];        if (lQueries !== null) {            lQueries.detachView(removedLView[TVIEW]);        }        viewToDetach[PARENT] = null;        viewToDetach[NEXT] = null;        // Unsets the attached flag        viewToDetach[FLAGS] &= ~128 /* Attached */;    }    return viewToDetach;}

待删除视图在container里的索引,通过CONTAINER_HEADER_OFFSET加上方法输入的removeIndex计算而成。

638289666735ee0c7507a07f8a3afae5.png

待删除的视图实例:

b8efd01c4de0e744efd5d4f539ff5166.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值