Skip to content

Commit 2092d2b

Browse files
committed
refactor(tree): avoid improper query usage
The way things are set up at the moment we have a `CdkNestedTreeNode` which uses a `ContentChildren` to find a `CdkTreeNodeOutlet` so that it knows where to render its content. On top of this we have a `MatNestedTreeNode` which extends `CdkNestedTreeNode` and defines it's own query at the same property, but looking for a `MatTreeNodeOutlet`. With this setup it means that both `CdkNestedTreeNode` and `MatNestedTreeNode` are trying to write to the same `nodeOutlet` property and it's by pure coincidence that it works. This is very fragile and it isn't guaranteed to work in future versions of the framework. These changes fix the issue by removing the query from the `MatNestedTreeNode` and providing the `MatTreeNodeOutlet` as a `CdkTreeNodeOutlet` so that the query on the `CdkNestedTreeNode` can pick it up.
1 parent f9ebb26 commit 2092d2b

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

src/material/tree/node.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ import {
1616
import {
1717
AfterContentInit,
1818
Attribute,
19-
ContentChildren,
2019
Directive,
2120
ElementRef,
2221
Input,
2322
IterableDiffers,
2423
OnDestroy,
25-
QueryList,
2624
} from '@angular/core';
2725
import {
2826
CanDisable,
@@ -33,8 +31,6 @@ import {
3331
mixinTabIndex,
3432
} from '@angular/material/core';
3533

36-
import {MatTreeNodeOutlet} from './outlet';
37-
3834
const _MatTreeNodeMixinBase: HasTabIndexCtor & CanDisableCtor & typeof CdkTreeNode =
3935
mixinTabIndex(mixinDisabled(CdkTreeNode));
4036

@@ -106,14 +102,6 @@ export class MatNestedTreeNode<T> extends _MatNestedTreeNodeMixinBase<T> impleme
106102
AfterContentInit, CanDisable, HasTabIndex, OnDestroy {
107103
@Input('matNestedTreeNode') node: T;
108104

109-
/** The children node placeholder. */
110-
@ContentChildren(MatTreeNodeOutlet, {
111-
// We need to use `descendants: true`, because Ivy will no longer match
112-
// indirect descendants if it's left as false.
113-
descendants: true
114-
})
115-
nodeOutlet: QueryList<MatTreeNodeOutlet>;
116-
117105
constructor(protected _elementRef: ElementRef<HTMLElement>,
118106
protected _tree: CdkTree<T>,
119107
protected _differs: IterableDiffers,

src/material/tree/outlet.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
* inside the outlet.
1919
*/
2020
@Directive({
21-
selector: '[matTreeNodeOutlet]'
21+
selector: '[matTreeNodeOutlet]',
22+
providers: [{
23+
provide: CdkTreeNodeOutlet,
24+
useExisting: MatTreeNodeOutlet
25+
}]
2226
})
2327
export class MatTreeNodeOutlet implements CdkTreeNodeOutlet {
2428
constructor(

tools/public_api_guard/material/tree.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export declare class MatNestedTreeNode<T> extends _MatNestedTreeNodeMixinBase<T>
33
protected _elementRef: ElementRef<HTMLElement>;
44
protected _tree: CdkTree<T>;
55
node: T;
6-
nodeOutlet: QueryList<MatTreeNodeOutlet>;
76
constructor(_elementRef: ElementRef<HTMLElement>, _tree: CdkTree<T>, _differs: IterableDiffers, tabIndex: string);
87
ngAfterContentInit(): void;
98
ngOnDestroy(): void;

0 commit comments

Comments
 (0)