8
私はまだAngular JSを学んでおり、異なるパラメータを使用してlastfm apiに2つのajaxリクエストを作成しています。私は各リクエストがいつ終了したかを知りたいので、両方のリクエストのローディングインジケータを表示することができます。私はそれを研究し、約束と$ qのサービスについて読んだが、これにこれを組み込む方法について頭を悩ませた。これを設定するより良い方法はありますか?どのように各要求が完了したかを知ることができます。ありがとう。Angularjs複数のajaxリクエストの最適化
angular.module('lastfm')
.controller('ProfileCtrl', function ($scope, ajaxData, usersSharedInformation, $routeParams) {
var username = $routeParams.user;
//Get Recent tracks
ajaxData.get({
method: 'user.getrecenttracks',
api_key: 'key would go here',
limit: 20,
user: username,
format: 'json'
})
.then(function (response) {
//Check reponse for error message
if (response.data.message) {
$scope.error = response.data.message;
} else {
$scope.songs = response.data.recenttracks.track;
}
});
//Get user info
ajaxData.get({
method: 'user.getInfo',
api_key: 'key would go here',
limit: 20,
user: username,
format: 'json'
})
.then(function (response) {
//Check reponse for error message
if (response.data.message) {
$scope.error = response.data.message;
} else {
$scope.user = response.data.user;
}
});
});
私はすべての要求
angular.module('lastfm')
.factory('ajaxData', function ($http, $q) {
return {
get: function (params) {
return $http.get('http://ws.audioscrobbler.com/2.0/', {
params : params
});
}
}
});
ああおかげでたくさん、理にかなって – Allan
喜んでそれが10ヶ月後...あなたのために働いていました – charlietfl