在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