我有一个返回值的 Angular 服务。我将该值推送到一个数组中,console.log 显示该值确实被推送了。但在此回调之外,数组不显示推送的值。如何使用推送的值保存此数组?
$scope.setAddons = function() {
var addons = new Array();
var arr = filterFilter($scope.addons, {checked: true});
for (var i = 0; i < arr.length; i++) {
element = arr[i].addoncode;
addons.push(element);
console.log(addons); // ["A6004"] great!
}
for (var x = 0; x < addons.length; x++) {
var dep = '';
myService.addon_dependencies(addons[x]).then(function(dependency) {
dep = dependency[0].addon_depend;
console.log(dep); // A6002
if(dep != null){
addons.push(dep);
console.log(addons); // ["A6004", "A6002"] great!
}
});
}
myService.addItem('addons', addons); //saving ["A6004"] instead of ["A6004", "A6002"]
console.log(addons); // ["A6004"] just shoot me!
location.path("/rates");
}