Ich habe eine Funktion in einem Controller und ich bin verwirrt, wie eine $ Scope-Variable innerhalb einer .then-Funktion aktualisiert wird.
Heres meine Funktion:
searchSpotify(query, $scope) {
this.Spotify.search(query,'track').then(function (data) {
console.log(data.tracks.items); // this is working
$scope.result = data;
});
}
in dem Konsolenprotokoll erhalte ich diese Fehlermeldung:.
TypeError: Cannot set property 'result' of undefined
Verwenden I $ Umfang irgendwie $ bewerben?
EDIT:
Hier ist die gesamte Steuerung für Kontext
(function() {
class SelectionController {
constructor(User, groupService, selectionService, songService, $scope, $routeParams, Spotify) {
this.groupId = $routeParams.groupId;
this.selectionId = $routeParams.selectionId;
this.groupService = groupService;
this.selectionService = selectionService
//this.selections = this.selectionService.getSelections();
this.songService = songService;
this.searchResults;
this.$scope = $scope;
this.Spotify = Spotify
}
searchSpotify(query) {
this.Spotify.search(query, 'track').then(function(data) {
console.log(data.tracks.items);
$scope.result = data;
});
}
addSong(name, artist, album) {
alert('called from selection controller');
this.songService.addSong(this.selectionId, name, artist, album);
}
createSelection(name, description) {
this.selectionService.createSelection(this.groupId, name, description);
}
deleteSelection(selection) {
this.selectionService.deleteSelection(selection);
}
}
angular.module('songSelectionApp')
.controller('SelectionController', SelectionController);
})();
verwenden sollten, wenn diese Funktion in einem Controller befindet, nicht Übergeben Sie die Variable $ scope an sie. – Noppey
die von 'then' als einen anderen Bereich aufgerufene Funktion. Sie können die Pfeilfunktion verwenden, wenn Sie ES6 verwenden, oder 'bind()' – rpadovani
, wenn ich die Variable $ scope nicht übergebe, bekomme ich den folgenden Fehler in der Konsole: "ReferenceError: $ scope ist nicht definiert" – Jcr