如何在 angular 4 中使用回调函数?

高拉巴伊达西尼

我曾经使用 google api 获取当前地址,现在我想使用 angular 4 在这个函数中实现回调函数如何实现它?

 let currgeocoder = new google.maps.Geocoder();
   currgeocoder.geocode({
      'location': location
   }, function(results:any, status:any) {
      if (status == google.maps.GeocoderStatus.OK) {
        let place = results[0];
       //this.showresult(place.formatted_address);
      } else {
          alert('Geocode was not successful for the following reason: ' + status);
      }
   });

      this.myGroup.setValue({
      searchControl: 'global'
    });
蛙蛙

您可以创建可观察对象并在其上推送新值,

let subject = new Subject(); 
let ovservable = subject.asObservable()

subject.next("b");

ovservable.subscribe((value) => {
  console.log("Subscription got", value); // Subscription wont get 
                                          // anything at this point
});

所以创建 observable ,公开它,当你从调用中接收数据时,使用.next()可以执行方法


在你的代码中

let subject = new Subject(); 
let ovservable = subject.asObservable();
let currgeocoder = new google.maps.Geocoder();
   currgeocoder.geocode({
      'location': location
   }, function(results:any, status:any) {
      if (status == google.maps.GeocoderStatus.OK) {
        let place = results[0];
        subject.next(place);
       //this.showresult(place.formatted_address);
      } else {
          alert('Geocode was not successful for the following reason: ' + status);
      }
   });

      this.myGroup.setValue({
      searchControl: 'global'
    });

    ovservable.subscribe((value) => {
      console.log("Subscription got", value); // Subscription wont get 
                                              // anything at this point
    });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章