@@ -14,6 +14,7 @@ import {
1414 QueryList ,
1515} from '@angular/core' ;
1616import { takeUntil } from 'rxjs/operators/takeUntil' ;
17+
1718import { CdkTree } from './tree' ;
1819import { CdkTreeNodeOutlet } from './outlet' ;
1920import { CdkTreeNode } from './node' ;
@@ -43,9 +44,9 @@ import {CdkTreeNode} from './node';
4344 'class' : 'cdk-tree-node cdk-nested-tree-node' ,
4445 'tabindex' : '0' ,
4546 } ,
47+ providers : [ { provide : CdkTreeNode , useExisting : CdkNestedTreeNode } ]
4648} )
4749export class CdkNestedTreeNode < T > extends CdkTreeNode < T > implements AfterContentInit , OnDestroy {
48-
4950 /** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
5051 protected _children : T [ ] ;
5152
@@ -59,14 +60,16 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
5960
6061 ngAfterContentInit ( ) {
6162 this . _tree . treeControl . getChildren ( this . data ) . pipe ( takeUntil ( this . _destroyed ) )
62- . subscribe ( result => {
63- // In case when nodeOutlet is not in the DOM when children changes, save it in the node
64- // and add to nodeOutlet when it's available.
65- this . _children = result as T [ ] ;
66- this . _addChildrenNodes ( ) ;
67- } ) ;
63+ . subscribe ( result => {
64+ if ( result && result . length ) {
65+ // In case when nodeOutlet is not in the DOM when children changes, save it in the node
66+ // and add to nodeOutlet when it's available.
67+ this . _children = result as T [ ] ;
68+ this . _addChildrenNodes ( ) ;
69+ }
70+ } ) ;
6871 this . nodeOutlet . changes . pipe ( takeUntil ( this . _destroyed ) )
69- . subscribe ( ( _ ) => this . _addChildrenNodes ( ) ) ;
72+ . subscribe ( ( _ ) => this . _addChildrenNodes ( ) ) ;
7073 }
7174
7275 ngOnDestroy ( ) {
@@ -78,7 +81,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
7881 /** Add children dataNodes to the NodeOutlet */
7982 protected _addChildrenNodes ( ) : void {
8083 this . _clear ( ) ;
81- if ( this . nodeOutlet . length && this . _children ) {
84+ if ( this . nodeOutlet . length && this . _children && this . _children . length ) {
8285 this . _children . forEach ( ( child , index ) => {
8386 this . _tree . insertNode ( child , index , this . nodeOutlet . first . viewContainer ) ;
8487 } ) ;
@@ -87,7 +90,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
8790
8891 /** Clear the children dataNodes. */
8992 protected _clear ( ) : void {
90- if ( this . nodeOutlet . first ) {
93+ if ( this . nodeOutlet && this . nodeOutlet . first ) {
9194 this . nodeOutlet . first . viewContainer . clear ( ) ;
9295 }
9396 }
0 commit comments