私はいくつかのJSONを取得するために新しいwindow.Fetchメソッドを使用しています。これは、私がconsole.logでjsonデータを読むことができる約束オブジェクトを返します。角度2コンポーネント内でPromiseの結果を使用するにはどうすればよいですか?
角度コンポーネント内でfetchメソッドを使用すると、結果が返されたときに他のメソッドを呼び出すことができません。スコープが何らかの形で失われているようですか?
以下のコメントを参照してください:字句のコンテキストを維持するために
(function(app) {
app.Actorlist =
ng.core.Component({
selector: 'actor-list',
templateUrl: './app/actorlist.component.html'
})
.Class({
constructor: function() {
},
showActors: function(js) {
this.actorName = js.name;
},
searchActor: function(){
fetch('http://swapi.co/api/people/1/')
.then(function(response) {
return response.json();
})
.then(function(js) {
// this logs the json result correctly
console.log(js);
// error: showActors is not defined
showActors(js);
// error: this.showActors is not defined
this.showActors(js);
// should I use return? return to where?
return js;
});
}
});
})(window.app || (window.app = {}));
['.bind()']( 'https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind)'にコールバックを 'this'にします。 –
"this.showActors(js).bind(this);"を使用すると、私は同じエラー "this.showActorsは関数ではありません"を取得します。私はまた、.bindがES6では必要なくなったと考えました... – Kokodoko
'.then(function(){bind}(this))'でなければなりません。 – dfsq