0

Hi i am very new to Angularjs .I am creating a table row using ng repeat ,row contains input type file so I wants to upload different file from different row . I wants to pass index for each row with onchange method .but index always return zero . Code snippet

input type="file" onchange="angular.element(this).scope().file_changed(this)" 

scope.file_changed = function (element) { 
var index = angular.element(element).scope().$index;
var files = element.files;

};
1
  • how your controller looks like? Commented Oct 13, 2018 at 5:10

1 Answer 1

1

I am not sure what's the intention behind of calling file_changed() method using angular.element. It's not the right approach in angular js. If this method already in your controller scope , you can access directly access from the html like below and you can pass $index as a parameter in to the method.

 <div ng-repeat="row in rows track by $index">
    <input type="file" onchange="file_changed(this,$index)" 
  </div>

I am assuming your controller will like this

  app.controller('YourController', function($scope) {
       $scope.file_changed=function(element,index){ 
            var files = element.files;
       }  
});
Sign up to request clarification or add additional context in comments.

4 Comments

Yes my controller is same like you have define above .I tried the solution you provided above but this did not help .throws this exception. In the following code I get 'Uncaught ReferenceError: $index is not defined' .
In the onchange attribute, the scope is only accessible via angular.element(this).scope().
@Suri did you try with track by $index in ng-repeat explicitly? check my answer
@Suri you are welcome. you can mark it as an answer if it’s works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.