BBa*_*ger 34 typescript angular
我想知道最好的方法,如果有不同的方法.我正在尝试从其父组件中调用子组件中的函数.所以,如果我有:
<parent>
<child></child>
</parent>
Run Code Online (Sandbox Code Playgroud)
...并且child具有调用的函数show()或者hide(),如何调用其中任何一个parent?
mxi*_*xii 78
在您的模板内:
<parent (click)="yourChild.hide()">
<child #yourChild></child>
</parent>
Run Code Online (Sandbox Code Playgroud)
在组件内部(option1):
import { ..., ViewChild, ... } from '@angular/core';
// ...
export class ParentComponent {
@ViewChild('yourChild') child;
ngAfterViewInit() {
console.log('only after THIS EVENT "child" is usable!!');
this.child.hide();
}
}
Run Code Online (Sandbox Code Playgroud)
在您的组件内部(option2):
import { ..., ViewChild, ... } from '@angular/core';
import { ..., ChildComponent, ... } from '....';
// ...
export class ParentComponent {
@ViewChild(ChildComponent) child;
ngAfterViewInit() {
console.log('only after THIS EVENT "child" is usable!!');
this.child.hide();
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅官方文档:https://angular.io/docs/ts/latest/cookbook/component-communication.html#!# parent-to-view- child
现场演示plunker:https://plnkr.co/edit/fEYwlKlkMbPdfx9IGXGR ? p = preview
| 归档时间: |
|
| 查看次数: |
39642 次 |
| 最近记录: |