简体   繁体   English

可变变量可通过闭包访问

[英]Mutable variable is accessible from closure in angular

I know there are already questions to this problem but I couldn't make out the solution in my case. 我知道这个问题已经存在疑问,但是我无法解决这个问题。 I want to reset a $scope value after using it, but I can't access i in the callback function. 我想在使用后重置$ scope值,但是无法在回调函数中访问i。

for (var i = 0; i < $scope.objects.length; i++) {
    Priority.create({
        "priority": $scope.selectedPriority[i].priority,
    }, function (priority) {
        $scope.selectedPriority[i].priority = undefined;
    });
}
function callbackFor(i, priority) {
      $scope.selectedPriority[i].priority = undefined;
    }

for (var i = 0; i < $scope.objects.length; i++) {
    Priority.create({
        "priority": $scope.selectedPriority[i].priority,
    }, callbackFor(i, priority));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM