Invoke click event file input after $mdDialog closes

I’m facing a strange behavior trying to invoke a file input by using his click event after close a $mdDialog.

When I invoke the click event without using the $mdDialog everything works fine, but using with the dialog the upload file choose popup never show.

Html code

<input type="file" id="file" ng-hide="true" />

<button ng-click="tryUploadFile($event)" />

JavaScript code

$scope.tryUploadFile = function(event) {
    upload(angular.element(document.body), 'Upload file', 'Cancel', event);

function upload(_parent, _okText, _cancelText, _event) {

    if($scope.hasCurrentChanges) {
        confirm = $mdDialog.confirm()
            .title('Are you sure you want to update?')
            .content('This item is already in use, updates maybe change its behavior.')

    else {

function openFileChooser() {
    // Opens the file selector

When goes to the else case everything works fine, but when the dialog shows up to confirm users operation the file input doesn’t show.

PS: the function openFileChooser is called correctly in both cases. I put the console.log inside to make sure.

Any one have any idea why this happen?

Thank you guys.


The reason is that for security reasons browsers will only action a .click() on a file input if the invocation of that .click() occurred from a real click event. If you want to use mdDialog, instead of using a confirm mdDialog you’d need to use a custom mdDialog with a custom action ‘ok/confirm’ button to fire your .click().

Something along the lines of:

$scope.ShowConfirm = function (ev) {
           controller: DialogController,
           template: '
            <md-dialog aria-label="Confirm" ng-cloak>
                    <div class="md-dialog-content">
                <md-dialog-actions layout="row">
                    <span flex></span>
                    <md-button ng-click="cancel()">
                    <md-button ng-click="open()" style="margin-right:20px;">
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose: true,
            fullscreen: false
    function DialogController($scope, $mdDialog) {
        $scope.cancel = function () {
        $ = function () {

Leave a Reply

Your email address will not be published. Required fields are marked *