我有一个控制器内的函数,我很困惑如何更新.then函数内的$ scope变量。
我的继承人功能:
searchSpotify(query, $scope) {
this.Spotify.search(query,'track').then(function (data) {
console.log(data.tracks.items); // this is working
$scope.result = data;
});
}
在控制台日志中我收到此错误:
TypeError: Cannot set property 'result' of undefined
我用$范围$莫名其妙申请?
编辑:
这里的上下文
(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);
})();
如果该功能位于控制器,不将$ scope变量传递给它。 – Noppey
由'then'调用的函数作为不同的范围。如果你使用的是ES6,你可以使用箭头函数,或者当我不通过$ scope变量时使用'bind()' – rpadovani
,我在控制台中得到以下错误:“ReferenceError:$ scope is not defined” – Jcr