【发布时间】:2014-06-23 07:39:11
【问题描述】:
我正在使用 Soundcloud JS SDK 将我的 Soundcloud 收藏夹带入一个简单的 Angular 应用程序中。
在我使用$scope.$apply 之前,我无法正确导入用户收藏夹。
function TopListCtrl($scope, $http, $modal) {
$scope.getData = function(sc_user) {
SC.get('/users/'+ sc_user +'/favorites', {limit: 200}, function(tracks){
$scope.$apply(function() {
if (Object.getOwnPropertyNames(tracks).length > 1) {
$scope.likes = tracks;
$scope.sortField = 'like.favoritings_count';
$scope.reverse = true;
$scope.sc_user = sc_user;
}
else {
alert("That user has 0 Soundcloud likes. So sad...")
}
}).error(function (data, status, headers, config) {
alert("Something went awry with that request. Double check that's a real Soundcloud username");
})
});
}
如果你不使用 $scope.apply,它就不起作用(并且说 SC.get not defined)。
我想更好地理解为什么$scope.$apply 是必要的。我问这个是因为当我只是使用http api时,我不需要它。
function TopListCtrl($scope, $http, $modal) {
$scope.getData = function(sc_user) {
var url = 'http://api.soundcloud.com/users/'+ sc_user +'/favorites.json?client_id=0553ef1b721e4783feda4f4fe6611d04&limit=200&linked_partitioning=1&callback=JSON_CALLBACK';
$http.jsonp(url).success(function(data) {
if (Object.keys(data.collection).length > 0) {
$scope.likes = data;
$scope.sortField = 'like.favoritings_count';
$scope.reverse = true;
$scope.sc_user = sc_user;
}
else {
alert("That user has 0 Soundcloud likes. So sad...")
}
}).error(function (data, status, headers, config) {
alert("Something went awry with that request. Double check that's a real Soundcloud username");
});
}
【问题讨论】:
-
相当简单...您正在使用代码更新不属于角度的范围,因此它不知道运行摘要循环
标签: angularjs angularjs-scope soundcloud