javascriptangularjstypescriptangular-materialmd-autocomplete

md-autocomplete in md-dialog loses focus onclick


I am working with AngularJS 1.6 and Angular Material I have an md-autocomplete which used to work really fine, but after i put it in an md-dialog box, it's not working so well anymore. Whenever i click on the autocomplete field, the focus disappears instantly and i can't fill the input.(or you have to be really fast to write a letter before the focus disappears).

I tried to do event.stopPropagation() on click, on focus and on keyup but it didn't help. I thought for a moment that the scopes of two controllers were in conflit so i removed the link between the two scopes and it didn't help. I searched a bit and nobody seems to have encoutered this before.

Here is the code that triggers the md-dialog :

    public showAddAuthDialog(ev: any) {

        this.$mdDialog.show({
            controller: AddJobAuthDialogController,
            controllerAs: "AddAuthDialog",
            templateUrl: 'public/app/views/dialogs/addAuthDialog.html',                
            targetEvent: ev,                
            clickOutsideToClose: true,
            fullscreen: this.customFullscreen,
            scope: this.$scope,
            preserveScope:true            
        });
    }    

Note : I pass the scope to keep the data, i thought this was the problem so i removed it and nothing changed.

Here is the code of the controller of the dialog :

     export class AddJobAuthDialogController {
         static $inject = ['$scope','$mdDialog',
              '$timeout',
                '$q',
                 'jobService'];
         public simulateQuery: boolean;
         public selectedItem: any;
         public selectedItemId: string;
         public searchTxt: string;      
         public jobsToSearch: any;            
         public selectedItemAD: any;
         public searchTxtAD: string;            
         public grpsToSearch: any;
         public customFullscreen = false;



    constructor(public $scope: any,
        public $mdDialog: any,
        public $timeout: any,
        public $q: any,
        public jobService: jobs.IJobService) {

        this.simulateQuery = true;
        this.selectedItem = null;
        this.searchTxt = null;
        this.selectedItemAD = null;
        this.searchTxtAD = null;

        this.jobsToSearch = this.$scope.Administration.jobsToSearch;
        this.grpsToSearch = this.$scope.Administration.grpsToSearch;

    }

    public $onChanges(changes) {
        if (changes.selectedItem) {
            this.selectedItem = changes.selectedItem.currentValue;
        }
        if (changes.jobsToSearch) {
            this.jobsToSearch = changes.jobsToSearch.currentValue;
        }
        if (changes.selectedItemId) {
            this.selectedItemId = changes.selectedItemId.currentValue;
        }
        if (changes.grpsToSearch) {
            this.grpsToSearch = changes.grpsToSearch.currentValue;
        }
    }

    public sendForm(event: any) {
        this.jobService.saveNewEntry(this.selectedItem.value, this.selectedItemAD.value).then((d) => {
            console.log(d);
            if (d.data == -1)
                this.showError(event);
            else
                this.showSuccess(event);
        });
    }

    public showSuccess(ev: any) {
        this.$mdDialog.show({
            controller: InsertDialogController,
            controllerAs: "InsertDialog",
            templateUrl: 'public/app/views/dialogs/adminInsertSuccessDialog.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose: true,
            fullscreen: this.customFullscreen // Only for -xs, -sm breakpoints.
        });
    };

    public showError(ev: any) {
        this.$mdDialog.show({
            controller: InsertDialogController,
            controllerAs: "InsertDialog",
            templateUrl: 'public/app/views/dialogs/adminInsertFailDialog.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose: true,
            fullscreen: this.customFullscreen // Only for -xs, -sm breakpoints.
        });
    };

    public querySearch(query, objectToSearch) {
        var results = query ? objectToSearch.filter(this.createFilterFor(query)) : objectToSearch,
            deferred;
        if (this.simulateQuery) {
            deferred = this.$q.defer();
            this.$timeout(function () { deferred.resolve(results); }, Math.random() * 1000, false);
            return deferred.promise;
        } else {
            return results;
        }
    }

    public createFilterFor(query) {
        var lowercaseQuery = angular.lowercase(query);
        return (res) => {
            return (angular.lowercase(res.display).indexOf(lowercaseQuery) !== -1);
        };
    }

    public cancelDialog() {
        this.$mdDialog.cancel();
    }
}

And here is the html template :

    <md-dialog class="dialogSuccess" aria-label="Add Groups To Job">

    <md-toolbar>
        <div class="md-toolbar-tools">
            <h2>Add Groups To Job</h2>
            <span flex></span>
            <md-button class="md-icon-button" ng-click="AddAuthDialog.cancelDialog()">
                <md-icon class="closeIcon" md-font-icon="mdi mdi-close" aria-label="Close dialog"></md-icon>
            </md-button>
        </div>
    </md-toolbar>

    <md-content layout="column" flex ng-cloak>
        <div class="md-dialog-content">
            <form>
                <md-autocomplete flex
                                 md-input-name="autocompleteField"
                                 md-input-minlength="0"
                                 md-input-maxlength="18"
                                 md-no-cache="true"
                                 md-selected-item="AddAuthDialog.selectedItem"
                                 md-search-text="AddAuthDialog.searchTxt"
                                 md-items="item in AddAuthDialog.querySearch(AddAuthDialog.searchTxt,AddAuthDialog.jobsToSearch)"
                                 md-item-text="item.display"                                                                                        
                                 md-floating-label="Jobs">
                    <md-item-template>
                        <span md-highlight-text="AddAuthDialog.searchTxt">{{item.display}}</span>
                    </md-item-template>
                    <md-not-found>Job "{{AddAuthDialog.searchTxt}}" not found </md-not-found>
                </md-autocomplete>

                <md-autocomplete flex
                                 md-input-name="autocompleteFieldAD"
                                 md-input-minlength="3"
                                 md-input-maxlength="18"
                                 md-no-cache="true"
                                 md-selected-item="AddAuthDialog.selectedItemAD"
                                 md-search-text="AddAuthDialog.searchTxtAD"
                                 md-items="i in AddAuthDialog.querySearch(AddAuthDialog.searchTxtAD,AddAuthDialog.grpsToSearch)"
                                 md-item-text="i.display"
                                 md-floating-label="AD Groups">
                    <md-item-template>
                        <span md-highlight-text="AddAuthDialog.searchTxtAD">{{i.display}}</span>
                    </md-item-template>
                    <md-not-found>Job "{{AddAuthDialog.searchTxtAD}}" not found </md-not-found>
                </md-autocomplete>

            </form>
        </div>
    </md-content>


<md-dialog-actions>
    <md-button class="md-raised md-primary md-hue-1" ng-click="AddAuthDialog.sendForm()">Add</md-button>
</md-dialog-actions>

 </md-dialog>

Note : For a moment i thought the problem was the floating label because i had the following warning in my Console :

mdInput messages show animation called on invalid messages element: JQLite [md-input-container.ng-scope.md-input-focused.md-input-has-value.ng-animate.md-input-invalid]

then i removed it to put a placeholder instead but the behaviour didn't change. Any ideas where the problem might come from?

UPDATE :

It seems that the focus is lost because it refocuses on the button that has been clicked, if i unfocus the button, every event on the page doesn't trigger anymore.

Here is the html code of the button :

      <div class="fab-container" layout="row" layout-align="end end">
    <md-fab-speed-dial md-open="false" md-direction="up"
                       ng-class="md-fling">
        <md-fab-trigger ng-click="Administration.showAddAuthDialog($event)" ng-focus="false">
            <md-button aria-label="menu" class="md-fab md-accent">
                <md-icon class="mdi mdi-plus fab-button-icon"></md-icon>
            </md-button>
        </md-fab-trigger>          
    </md-fab-speed-dial>
</div>

Solution

  • It seems that the problem was the md-fab-speed-dial, which for an unknown reason stays focused no matter what. I just replaced the whole thing with a simple button and now it works just fine.

    The new html :

        <div layout="row" layout-align="end end">       
                <md-button  ng-click="Administration.showAddAuthDialog($event)" aria-label="menu" class="md-fab md-accent">
                    <md-icon class="mdi mdi-plus fab-button-icon"></md-icon>                    
                </md-button>          
    </div>