Angular: Empty Fiddle
http://angularjs.org/
by maksym123
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
Hello, {{name}}!
</div>
JavaScript
var myApp = angular.module('myApp',[]);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope, $q, $http) {
// Promise function that knows how to download a single part
function downloadPart(myUrl) {
// return http promise
return $http({
method: 'GET',
url: myUrl
});
};
// Aggregat epromise that downloads all parts
function downloadAllParts(parts) {
var partsPromises = []; // Setup array for indivudual part promises
angular.forEach(parts, function(part) { // Iterate through each part
// Schedule download of a single
partsPromises.push(downloadPart(part));
});
// Wait for all parts to resolve
return $q.all(partsPromises)
.then(function(data) {
var resData = [];
console.log(data);
// Returned data will be an array of results from each individual http promise
angular.forEach(data, function(partData) {
resData.push(partData.data);
})
return resData;
});
};
var myParts = ['/echo/js/?js={"a":"bb"}', '/echo/js/?js={"bb":"cccc"}'];
downloadAllParts(myParts)
.then(function(data) {
alert('Success: ' + data[0].a + ' ' + data[1].bb);
}, function(error) {
console.log('eerr');
alert(error);
})
}