Skip to content

Commit 15d7037

Browse files
author
Bert Vanhooff
committed
Adds instructions for getting child pages.
1 parent 74c7cba commit 15d7037

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ng2-notes
22

3-
Some notes on the stuff I struggeled with learning Angular 2 (0.46)
3+
Some notes on the stuff I struggeled with learning Angular 2 (beta0)
44

55
## Adding content inside a component/directive (transclusion)
66

@@ -81,7 +81,51 @@ modalLoaded(modal: Modal) {
8181
this.modal = modal;
8282
}
8383
}
84+
```
8485

86+
## Getting the (view) children of a component
8587

88+
Suppose you're making a wizard component with child pages and you need to refer the children from the parent component.
89+
``` xml
90+
<wizard>
91+
<wizard-page [label]="page1"></wizard-page>
92+
<wizard-page [label]="page2"></wizard-page>
93+
</wizard>
94+
```
8695

96+
The wizard needs to be able to do something like:
97+
```java
98+
this.activePage = wizardPages[0];
99+
wizardPages[0].isValid();
100+
[...]
87101
```
102+
103+
``` java
104+
@Component({
105+
selector: 'wizard'
106+
})
107+
@View({
108+
directives: [WizardPage]
109+
})
110+
export class Wizard implements AfterViewInit {
111+
112+
// This is the key: viewChildrew will contain all the wizard pages.
113+
@ViewChildren(WizardPage) viewChildren: QueryList<WizardPage>;
114+
115+
// Viewchildren (the WizardPages) are available now.
116+
ngAfterViewInit() {
117+
// viewChild is updated after the view has been initialized
118+
for(let child of this.viewChildren.toArray()) {
119+
console.log(child);
120+
}
121+
}
122+
}
123+
124+
@Component({
125+
selector: 'wizard-page'
126+
})
127+
export class WizardPage implements OnInit {
128+
[...]
129+
}
130+
```
131+

0 commit comments

Comments
 (0)