递归函数我有这样的代码:AngularJS上承诺
$scope.play = function (audio) {
var deferred = $q.defer();
api.play(audio).then(function() {
deferred.resolve();
}, function (err) {
deferred.reject();
console.log(err);
});
return deferred.promise;
};
我有audio
,我使用我的控制器内,并传递给$scope.play
一个阵列。但是,使用下面:
var playIndex = 0;
$scope.play(audios[playIndex]).then(function() {
playIndex++;
$scope.play(audios[playIndex]);
});
这适用于前两种音频我当然有数组中,因为我不检查第二.play
的回报。我怎样才能使这个循环成功通过所有的音频阵列?
在此先感谢。
包装现有承诺是一种反模式。你的函数应该是'$ scope.play = function(audio){return api.play(audio); }'。就是这样,真的。 –