AngularJS v1.5.8
<input type="file" onchange="angular.element(this).scope().uploadImage()" />
$scope.uploadImage = function(){
alert("change");
}
控制台中出现错误:
未捕获的TypeError:无法读取属性&#39; uploadImage&#39;未定义的 在HTMLInputElement.onchange
答案 0 :(得分:0)
我认为你设置了角度错误,它应该像这样工作: -
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.uploadImage = function() {
alert("change");
}
});
&#13;
<!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>
&#13;
答案 1 :(得分:0)
尝试如下所示:
<强> 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];
}
请参阅:jsbin