@@ -14,6 +14,7 @@ import {
14
14
QueryList ,
15
15
} from '@angular/core' ;
16
16
import { takeUntil } from 'rxjs/operators/takeUntil' ;
17
+
17
18
import { CdkTree } from './tree' ;
18
19
import { CdkTreeNodeOutlet } from './outlet' ;
19
20
import { CdkTreeNode } from './node' ;
@@ -43,9 +44,9 @@ import {CdkTreeNode} from './node';
43
44
'class' : 'cdk-tree-node cdk-nested-tree-node' ,
44
45
'tabindex' : '0' ,
45
46
} ,
47
+ providers : [ { provide : CdkTreeNode , useExisting : CdkNestedTreeNode } ]
46
48
} )
47
49
export class CdkNestedTreeNode < T > extends CdkTreeNode < T > implements AfterContentInit , OnDestroy {
48
-
49
50
/** The children data dataNodes of current node. They will be placed in `CdkTreeNodeOutlet`. */
50
51
protected _children : T [ ] ;
51
52
@@ -59,14 +60,16 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
59
60
60
61
ngAfterContentInit ( ) {
61
62
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
+ } ) ;
68
71
this . nodeOutlet . changes . pipe ( takeUntil ( this . _destroyed ) )
69
- . subscribe ( ( _ ) => this . _addChildrenNodes ( ) ) ;
72
+ . subscribe ( ( _ ) => this . _addChildrenNodes ( ) ) ;
70
73
}
71
74
72
75
ngOnDestroy ( ) {
@@ -78,7 +81,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
78
81
/** Add children dataNodes to the NodeOutlet */
79
82
protected _addChildrenNodes ( ) : void {
80
83
this . _clear ( ) ;
81
- if ( this . nodeOutlet . length && this . _children ) {
84
+ if ( this . nodeOutlet . length && this . _children && this . _children . length ) {
82
85
this . _children . forEach ( ( child , index ) => {
83
86
this . _tree . insertNode ( child , index , this . nodeOutlet . first . viewContainer ) ;
84
87
} ) ;
@@ -87,7 +90,7 @@ export class CdkNestedTreeNode<T> extends CdkTreeNode<T> implements AfterContent
87
90
88
91
/** Clear the children dataNodes. */
89
92
protected _clear ( ) : void {
90
- if ( this . nodeOutlet . first ) {
93
+ if ( this . nodeOutlet && this . nodeOutlet . first ) {
91
94
this . nodeOutlet . first . viewContainer . clear ( ) ;
92
95
}
93
96
}
0 commit comments