2

i using SignaturePed js plugin to implement signature in my site. i have a multi signatures types and multiple users. in this for i run on all users and tutch SignaturePed class for each canvas elements

for (var i = 0; i < $scope.summary.insurance.members.length; i++) {

    if ($scope.summary.insurance.members[i].Age >= 18) {

        if ($scope.summary.insurance.payment.IdentityNo == $scope.summary.insurance.members[i].IdentityNo && $scope.summary.localPolicy.Action == 0) { //payer only

            $scope.summary.signaturePad[i] = {

                1: new SignaturePad($scope.summary.canvas[i][1], {
                    'onEnd': function() {
                        $scope.summary.saveSignOnBlur(1, i)
                    }
                }),

                2: new SignaturePad($scope.summary.canvas[i][2], {
                    'onEnd': function() {
                        $scope.summary.saveSignOnBlur(2, i)
                    }
                }),

                3: new SignaturePad($scope.summary.canvas[i][3], {
                    'onEnd': function() {
                        $scope.summary.saveSignOnBlur(3, i)
                    }
                }),

            }

        } else {

            $scope.summary.signaturePad[i] = {

                1: new SignaturePad($scope.summary.canvas[i][1], {
                    'onEnd': function() {
                        $scope.summary.saveSignOnBlur(1, i)
                    }
                }),

                2: new SignaturePad($scope.summary.canvas[i][2], {
                    'onEnd': function() {
                        $scope.summary.saveSignOnBlur(2, i)
                    }
                }),

            }

        }

    }

}

my problem is that when the callback run the index (i) value is 2 (for 2 users) because that on end the loop this is index value

1 Answers1

2

As are you can see in the comments for this problem there are two solutions First easy solution: using let for index deceleration to index variable be local (in fact in every iteration is a new variable), the defect in this solution is that let supported only from es6 hence not work in explorer or edge. Because i need to this script will work in explorer i have implemented the second closure solution and with closure will a new scope in every iteration so:

for(var i = 0;i < ...;i++){ (function(i){...})(i); }