1

I have a login modal in a directive which I am trying to make it work using link function and jquery but it doesn't seem to work.

  • This is the login button

    <button login-modal type="button" class="btn">Sign In</button>
    
  • This is the smaller version of the login modal view

    <div id="loginModal" class="modal fade" role="dialog"></div>
    
  • This is the login directive

    angular
        .module('app')
        .directive('loginModal', ['$scope', loginModalDirective]);
    
    
        function loginModalDirective($scope) {
        return {
            restrict: 'E',
            templateUrl: 'template/loginmodal.view.html',
            link: function(scope, elem, attr) {
                elem.on('click', function() {
                    jQuery("#loginModal").modal();
                });
            }
        };
    }
    
4
  • 1
    You should seriously consider using AngularJS for handling events instead of jQuery. For instance, instead of doing elem.on('click' use the ng-click directive. Commented Jan 19, 2017 at 18:17
  • That's actually a good idea Mike but I have a part on my website where I'll be using tons of the same modal and I thought this way might be better. I am new to angular though so I might even be wrong. Commented Jan 19, 2017 at 18:29
  • @S.Patel That actually a reason that you should be using Angular. You can define a modal template (including a view and controller) just the same as you would for a full state and then pass it around to anything that needs to call up an instance of it. I'd recommend the Angular Bootstrap modal. Commented Jan 19, 2017 at 19:45
  • @HarrisWeinstein OMG! TY SO MUCH Commented Jan 20, 2017 at 14:23

1 Answer 1

4

You have the restriction to E, which means element. Your element is button. Your directive is not being initialized.

Use A for attribute, since login-modal is an attribute.

restrict: 'A'

Also (not a problem, just waste), you don't use $scope and there's no need to inject into the directive. You get scope in the link method already.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.