0

AngularJS v1.5.8

<input type="file"   onchange="angular.element(this).scope().uploadImage()" />

$scope.uploadImage = function(){
            alert("change");
        }

error in console:

Uncaught TypeError: Cannot read property 'uploadImage' of undefined at HTMLInputElement.onchange

georgeawg
  • 48,608
  • 13
  • 72
  • 95
VenbillYu
  • 1
  • 1
  • i got answer html controller angular.element(document.body).scope.uploadImage = function() { alert("change"); } thanks all – VenbillYu Aug 16 '17 at 03:51

2 Answers2

0

I think you have setup angular wrong, it should work like this:-

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {

  $scope.uploadImage = function() {
    alert("change");
  }
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="myCtrl">

    <input type="file" onchange="angular.element(this).scope().uploadImage()" />

  </div>
Gaurav Srivastava
  • 3,232
  • 3
  • 16
  • 36
0

Try as like as given below :

HTML

  <input
       type="file"
       ng-model="file"
       name="file"                                
       onchange="angular.element(this).scope().uploadImage(this)"
                    />

AngularJS

  $scope.uploadImage = function(elements) {

      let file =  elements.files[0];   
  }

See : jsbin

sabbir
  • 2,020
  • 3
  • 26
  • 48