1

我正在使用 angular 4。我的应用程序中有父路由和子路由。

在父母中,我有 2 个按钮->“添加”和“删除”。

当在父路由中单击“添加”按钮时,我想调用我的子组件的函数。我不知道如何实现这一点。

任何帮助,将不胜感激。

4

1 回答 1

1

您可以简单地为此使用模板变量。

// child.component.ts
@Component({ selector: 'child', template: '...' })
export class ChildComponent {
    public someFunctionToCall() {...}
}

// parent.component.html
<div>
   <child #childComponentRef></child>

   <button (click)="childComponentRef.someFunctionToCall()">Add</button>
</div>
于 2017-12-01T12:55:47.493 回答