i have in my controller a function that executes some tasks, one of them is to check using my service if some prices or valid, and if there exist any errors stop from executing the rest of the code, the problem is that my variable "error" isnt being updated, basically in my promise when there is a error it increments the error variable, and then after this case there are any errors returns (stop). But cant figure out why isnt updating the variable error.
My code:
function apply(){
var error = 0;
if(vm.costPrice){
vm.name="";
for (var i = 0; i < vm.costPrice.length; i++) {
(function (i) {
if (isChecked(vm.costPrice[i].id)) {
//create later a service that accepts array of data (less requests = better performance)
MyService.validateThings(vm.product.pvp, vm.costPrice[i].discount_price)
.then(function (response) {
},
function (response) {
error++;
vm.isErrorCostPrice = true;
if (i > 0) {
vm.name += ' ';
}
vm.name += vm.costPrice[i].condition.name;
});
}
}(i));
}
if(error > 0){
return;
}
// Other code above...
}