How to open select programmatically

前端 未结 4 1513
醉梦人生
醉梦人生 2021-01-17 10:07

Does anyone know if it is possible to open a select programmatically in angularjs. Ive tried

angular.element(el).trigger(\'focus\');
angular.element(el).trig         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-17 10:33

    i had same problem and finally i'm create my own directive:

    angular.module('openselect', [])
        .directive('openselect', function () {
            var showDropdown = function (element) {
                var event;
                event = document.createEvent('MouseEvents');
                event.initMouseEvent('mousedown', true, true, window);
                element.dispatchEvent(event);
            };
            return {
                restrict: 'A',
                scope: {
                    'elemento': '@'
                },
                link: function (scope, elem, attrs, ctrl) {
                    elem.bind('click', function () {
                        var elemento = document.getElementById(scope.elemento);
                        showDropdown(elemento);
                    });
                }
            };
        });
    

    To use:

    Indicanos tu país

    Adds directive to any tag passing id of select (elemento) that you want to open.

    You can see the very basic javscript code inside directive if you want to use in other context;

    hope it helps ;D

提交回复
热议问题