Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is my HTML:

<button class="btn btn-default" ng-click="selectFile()">Choose file</button>
<input type="file" accept="image/*" name="file" style="display: none;" id="photo-file" fileupload="uploadme">

In my controller, I have:

$scope.selectFile = function() {
  $("#photo-file").click();
}

I also created a directive:

angular.module("MyApp")
  .directive("fileupload", function($rootScope) {
    return {
      scope: {
        'fileupload': "="
      },
      link: function(scope, element, attributes) {
        return element.bind("change", function(changeEvent) {
          return scope.$apply(function() {
            return scope.fileread = changeEvent.target.files[0];
          });
        });
      }
    };
  }
);

But something is missing. When I select the file to upload, I want it to run a function in the controller. How can I do this?

share|improve this question
1  
The easiest way is probably to use scope.$parent.<Your function>. The directive creates a child scope of whatever controller you are using. –  Zack Argyle Nov 22 '13 at 17:18
    
You can $watch the variable you bind to fileupload in your controller and take action when it changes. –  musically_ut Nov 22 '13 at 18:28

1 Answer 1

up vote 1 down vote accepted

If you are okay with it, I can recommend using an external module. Here is something I tried and was pretty happy with it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.