Peter Boomsma
Peter Boomsma

Reputation: 9798

Can't use $mdSelect in angularjs directive

I've been working on a way to close the md-select when a md-menu is closed. I've got that figured out > https://codepen.io/anon/pen/jxXbrX.

But now I'm trying to implement that in my own application but I'm getting $mdSelect.hide is not a function message in my browser console.

This is the directive:

namespace Xxx{
    angular.module('Xxx')
    .directive('mdCloseSelect', ['$mdSelect', ($mdSelect) => {
        return {
            link (scope, element, attrs, $mdSelect) {
                scope.$on('$mdMenuClose', (ev, element, $mdSelect) => {

                    $mdSelect.hide();

                });
            }
        };
    }]);
}

I think the problem is with the declaration of $mdSelect but I can't find how I should declare it properly.

Upvotes: 0

Views: 46

Answers (1)

Petr Averyanov
Petr Averyanov

Reputation: 9476

This at least should fix undefined problem:

angular.module('Xxx')
.directive('mdCloseSelect', ['$mdSelect', ($mdSelect) => {
    return {
        link (scope, element, attrs) { // You can not inject here anything, this is always predefined 3 args
            scope.$on('$mdMenuClose', () => { // Here you also can not

                $mdSelect.hide();

            });
        }
    };
}]);

P.S. Use some checkstyle ike Eslint, that will tell you that using same names of vars is bad.

Upvotes: 1

Related Questions