首页 文章

当在父级的html中单击时,在Angular 2 / Ionic中调用子方法

提问于
浏览
1

我面对这种情况好几个小时,我找不到合适的答案 . 我正在使用角度2和离子2 .

我有 1 page (首页 - home.tshome.html

1 component (GoogleMaps, google-maps.tsgoogle-maps.html

home.html 我有这个:

<ion-header>
  <ion-navbar>
    <ion-buttons end>
      <button ion-button (click)="findMyLocation()">Find</button>
    </ion-buttons> 
  </ion-navbar>
</ion-header>

google-maps.ts 我有findMyLocation方法,但我不是't know how to trigger it in my component from home.html (parent'页面)

完整代码:http://plnkr.co/edit/Qcy4BKHgC1GBMrLJO7lj

错误是:无法读取未定义的属性'findMyLocation'

EDIT: 我在 home.ts 找到了这个代码的一种方法,但我希望另一个方法更容易?有?

@ViewChild(GoogleMaps) gmap: GoogleMaps;

  findMyLocation(){
    this.gmap.findMyLocation();
  }

1 回答

  • 2

    你可以试试这个:

    <google-maps #googlemaps></google-maps>
    

    然后像这样使用它:

    <button ion-button (click)="googlemaps.findMyLocation()">Find</button>
    

    我用一个简单的例子尝试了它并且它起作用了 . 到目前为止,我还没有在文档中找到任何内容,所以我无法告诉你这究竟是如何工作的 . 似乎#创建了一个局部变量,引用了您的组件,然后您可以在当前范围内使用该变量 .

相关问题