简体   繁体   English

如何在Angular 1.5中调用另一个组件方法

[英]How to call another component method in Angular 1.5

I am using Angular 1.5. 我正在使用Angular 1.5。 I have two components. 我有两个组成部分。 I wanted to call child component method from parent component view. 我想从父组件视图调用子组件方法。

<main-component>
    <child-component></child-component>
    <div ng-if="childComponentMethod()"></div>
<main-component>

如果需要在其他控制器中使用此方法,则应使用服务来共享此方法,或者应在父级中声明它,因为子级可以访问它。

The childComponentMethod method lives in child-component scope, so it is not available outside of it. childComponentMethod方法位于child-component范围内,因此在其外部不可用。 And what's more, you should not need it. 而且,您不需要它。 If you do, it means the method should not be in the child-component scope in the first place. 如果这样做,则意味着该方法首先不应位于child-component范围内。

If you need that method only here, just move it to the main-component scope. 如果仅在此处需要该方法,则将其移至main-component范围。 Otherwise, create a service and move the method there. 否则,请创建服务并将方法移到该处。 You can then inject the service where it is needed. 然后,您可以在需要的地方注入服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM