【问题标题】:how to call method in one component and it will call another method in another component?如何调用一个组件中的方法,它会调用另一个组件中的另一个方法?
【发布时间】:2020-07-02 17:41:34
【问题描述】:

我是 Angular 新手,所以组件扩展让我感到困惑。我有两个组件,例如标题和内容。所以我在标题组件中有一个类似 filterChange() 的方法,所以当我调用这个方法时,它应该调用另一个组件中的另一个方法。我尝试了@input 和@output,但在那里我无法调用这些方法。

头组件html:

<mat-radio-group[(ngModel)]="visibility" (ngModelChange)="filterChange()">
  <mat-radio-button [value]=null>true</mat-radio-button>
  <mat-radio-button *ngFor="let visibility of jobVisibilities"
                        [value]="visibility">false</mat-radio-button>
</mat-radio-group>

头组件ts:

filterChange(){
 }

这是我的代码。我也尝试过服务,但我不知道该怎么做。 这个怎么做?请帮我解决这个问题?

【问题讨论】:

    标签: typescript angular8


    【解决方案1】:
        Lets say u have 2 components ***HeaderComponent.ts*** and ***ContentComponent.ts***    
        U want to call the test() function from header component, which is in Content component
        
        * Import the ***content component*** 
        
        import {classname} from ./../content.component.ts;
        
        * Give the dependency injection
        
        constructor(){ public content : classname }
        
        filterChange(){
        
        /*you can call any content component function here*/
        
        let sample = this.content.test()
        console.log(sample)
        }
    

    【讨论】:

    • 哇!谢谢@lliyas...不使用服务我得到了这个答案。
    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 2018-09-06
    • 2017-12-09
    • 2017-08-19
    • 2017-05-19
    • 1970-01-01
    • 2015-12-15
    相关资源
    最近更新 更多