Angular2如何在另一个组件中使用单个组件功能

时间:2017-08-21 04:54:54

标签: angular typescript angular2-forms

这里我有2个像CacadingComponent和LoginComponent的组件我想在LoginComponent中使用CacadingComponent函数

CacadingComponent

export class CascadingComponent {
   CountryList: Country[];
   constructor(private _CacadeService: CascadindService) {       
   }
    GetCountry() {
        return this._CacadeService.GetCountryList().subscribe((data) => this.CountryList = data);
    }

LoginComponent.ts

这里我想在我的LoginComponent中调用GetCountry()函数

export class LoginComponent{
//Here How can I call
}

2 个答案:

答案 0 :(得分:3)

您可以通过制作类CascadingComponent的对象来访问方法,如下所示:

export class LoginComponent implements OnInit{

       cascadingComponent = new CascadingComponent();

       ngOnInit(){
         this.cascadingComponent.GetCountry();
      }
}

答案 1 :(得分:2)

    export class LoginComponent implements OnInit{
    @ViewChild(CascadingComponent) cascadingComponent: CascadingComponent ;
    ngOnInit(){
    this.cascading.GetCountry();}
}