I am using the new window.Fetch method to fetch some JSON. This returns a promise object, from which I can read the json data with console.log.
But if I use the fetch method inside an angular component, I am unable to call other methods when the result is returned. It seems that the scope is somehow lost?
See the comments below:
(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 = {}));