1

This is what I have in my controller.js

$scope.fileAdded = false;

$scope.fileNameChanged = function() {
    $scope.fileAdded = true;
};

This button I want to enable ...

<a ng-disabled="!fileAdded" class="btn btn-primary" role="button">Click</a>

... when I choose a file:

<input type="file" name="file" id="file"
       onchange="angular.element(this).scope().fileNameChanged(this)">

Although fileAdded changes from false to true, the button is not enabled. Why is it still disabled?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Justin Lange
  • 897
  • 10
  • 25
  • For better ways to do this, see [ng-model for ](https://stackoverflow.com/questions/17063000/ng-model-for-input-type-file/43074638#43074638). – georgeawg Jun 27 '18 at 13:14

2 Answers2

2

With onchange you are out of angularJS. So you need to trigger the digect cycle by yourself:

$scope.fileNameChanged = function() {
    $scope.fileAdded = true;
    $scope.$digest();
};
MrWook
  • 348
  • 3
  • 11
-1

You should use Angular's ngChange directive, as opposed to the DOM onchange.

<input type="file" name="file" id="file" ng-change="fileNameChanged()">
Protozoid
  • 1,207
  • 1
  • 8
  • 16