在Angular 2中,如何從父組件類訪問子組件類?例如如何在Angular2中從父組件類訪問子組件類
import {Component, View} from 'angular2/core';
@Component({selector: 'child'})
@View({template: `...`})
class Child {
doSomething() {
console.log('something');
}
}
@Component({selector: 'parent'})
@View({
directives: [Child],
template: `<child></child>`
})
class Parent {
constructor() {
//TODO: call child.doSomething() when the child component is ready
}
}
在這個例子中,我怎麼會叫Child
組件無論從Parent
組件的構造或一些回調函數doSomething()
方法。
在我的情況下,我只有一個子組件,所以我只是使用'this.children.first'。謝謝! – rob
@rob如果你只需要一個子組件,然後使用['@ ViewChild'](https://github.com/angular/angular/blob/2.0.0-alpha.45/modules/angular2/src/core/metadata /di.ts#L353)而不是'@ ViewChildren' – alexpods
哦,甚至更好。謝謝 – rob