2017-12-15 22 views
0

Google apiを使用して現在のアドレスを取得していましたが、この関数でコールバック関数を実装するには、角度4を使用します。角度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' 
    }); 
+0

あなたが何かを私に知らせる必要がある場合私の答えに見える –

答えて

1
あなたはoberservable作成し、その上に新しい値をプッシュすることができ

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 
}); 

ので、観察可能な作成、それを公開し、あなたが.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 
    }); 
+0

私はshowresult関数で結果[0]の値を渡したいので、それをどうすることができますか? – dgpoo

+0

asObservable()とは何ですか?任意のライブラリをインポートする必要がありますか? – dgpoo

+0

cmdでこのエラーが発生しています。 TS2304:名前 'Subject'が見つかりません。 – dgpoo