컨트롤러를 구문으로 사용하고 있습니다.입력의 onchange에서 함수 호출
그리고 내 파일 입력이 변경되면 기능을 트리거하고 싶습니다.
어떻게 수행하나요?
아래 $ 범위 구문
<input class="upload" type="file" accept="image/*" ng-model="image"
onchange="angular.element(this).scope().uploadImage(this)">
컨트롤러를 구문으로 사용하고 있습니다.입력의 onchange에서 함수 호출
그리고 내 파일 입력이 변경되면 기능을 트리거하고 싶습니다.
어떻게 수행하나요?
아래 $ 범위 구문
<input class="upload" type="file" accept="image/*" ng-model="image"
onchange="angular.element(this).scope().uploadImage(this)">
사용 scope.$watch
각도 내 코드입니다. 컨트롤러,이 추가 :
scope.$watch("image", function(newValue, oldValue) {
// Do anything you want here
scope.uploadImage(newValue);
}
에 대한 scope.$watch
더 많은 정보 :이 입력 변화
또는 HERE
, 사용자 정의 지시문을 사용하여 입력 파일의 변경 사항을 수신 할 수 있습니다.
view.html :
<input type="file" custom-on-change="uploadFile">
controller.js :
app.controller('myCtrl', function($scope){
$scope.uploadFile = function(event){
var files = event.target.files;
};
});
directive.js :
app.directive('customOnChange', function() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var onChangeHandler = scope.$eval(attrs.customOnChange);
element.bind('change', onChangeHandler);
}
};
});
나는'ng-change'가 다음과 같이''에서 작동한다고 생각하지 않습니다 : https://github.com/angular/angular.js/issues/1375 (나는 테스트를하지 않았습니다 ...) . 그러나 '각도 파일 업로드'는 실제로 가능한 해결책입니다. – user3632710
바와 같이 here을 발견 사용할 때 트리거이 ng-change="nameOfFunction()"
을 사용할 수 있습니다 https://docs.angularjs.org/api/ng/type/$rootScope.Scope
왜'NG 변화를 사용하지 = " yourController.yourFunction() "'? – Matheno
type = "file"인 경우 변경을 사용할 수 없습니다. –