jhipster webapp中的angularjs输入文件onchange错误

时间:2017-08-15 05:46:05

标签: angularjs jhipster

AngularJS v1.5.8

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

$scope.uploadImage = function(){
            alert("change");
        }
控制台中出现

错误:

  

未捕获的TypeError:无法读取属性&#39; uploadImage&#39;未定义的       在HTMLInputElement.onchange

2 个答案:

答案 0 :(得分:0)

我认为你设置了角度错误,它应该像这样工作: -

&#13;
&#13;
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;
&#13;
&#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