天天看點

Angular view container删除view執行個體的過程

單步調試進入this.viewContainer.createEmbeddedView(this.templateRef);

Angular view container删除view執行個體的過程
Angular view container删除view執行個體的過程
Angular view container删除view執行個體的過程
Angular view container删除view執行個體的過程
Angular view container删除view執行個體的過程

從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;

   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]));

       const removedLView = removeFromArray(lContainer, CONTAINER_HEADER_OFFSET + removeIndex);

       addRemoveViewFromContainer(viewToDetach[TVIEW], viewToDetach, false, null);

       // notify query that a view has been removed

       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計算而成。

Angular view container删除view執行個體的過程

待删除的視圖執行個體:

Angular view container删除view執行個體的過程

繼續閱讀