jQuery function in directive not working

SjaakvBrabant

My jQuery function isn't working inside my directive.

This is my directive:

app.directive('responsiveNav', function () {
  return {
      restrict: 'E',
      link: function (element) {
          var pull = $('#pull');
          var menu = $('.nav-bar ul ul');
          var menuHeight = menu.height();

          $(pull).on('click', function (e) {
              e.preventDefault();
              menu.slideToggle();
          });

          $(window).resize(function () {
              var w = $(window).width();
              if (w > 320 && menu.is(':hidden')) {
                  menu.removeAttr('style');
              }
          });
      }
  };
});

HTML:

<div class="header" responsive-nav>
        <div class="nav-bar">
            <ul style="padding:15 0 15 0;height:50px;">
                <li style="border-right: none;">
                    <a href="" style="padding:0;"><img src="assets/images/who-logo.png"></a>
                </li>
                <a href="#" id="pull">Menu</a>
                <ul style="float:right;list-style-type:none;padding:8.5 0 8.5 0;">
                    <li><a class="active" href="">Home</a></li>
                    <li><a href="">About us</a></li>
                    <li><a href="">Tickets</a></li>
                    <li><a href="">Event</a></li>
                    <li><a href="">Gallery</a></li>
                    <li><a href="">Video</a></li>
                    <li><a href="">Contact</a></li>
                    <li style="border-right: none;"><a><i class="fa fa-facebook" style="font-size: 1.5em;"></i></a></li>
                    <li style="border-right: none;"><a><i class="fa fa-instagram" style="font-size: 1.5em;"></i></a></li>
                    <li style="border-right: none;"><a><i class="fa fa-twitter" style="font-size: 1.5em;"></i></a></li>
                </ul>
            </ul>

        </div>
    </div>

The directive doesn't even react to the click on #pull and I have no errors in my console. Hope you understand the problem. Any help is welcome!

Charlie

Your directive is restricted to Element by

 restrict: 'E'

But you have put the directive as an attribute.

<div class="header" responsive-nav>

restrict: 'A' is one way to solve the problem.

Also I don't agree some of the ways you are using jquery here. Try to be angular as much as possible.

Please take some time to refer the angular directive documentation to make better use of this powerful feature of the framework.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Google Maps for Angular directive not working in function

分類Dev

jQuery .post() function not working

分類Dev

jQuery click(function) not working

分類Dev

click function not working in JQuery

分類Dev

jQuery width function parameters not working

分類Dev

Reason for jQuery Cycle function not working

分類Dev

jQuery function working with more data

分類Dev

jquery click function not working for me

分類Dev

$.ajax is not a function, but the jquery library is working

分類Dev

custom directive not working

分類Dev

Jquery function on Dynamically created List not working

分類Dev

jQuery function .split() not working, AJAX data not submitted

分類Dev

jQuery find className not working for second click function

分類Dev

RegEx not working in directive - Angular 4

分類Dev

AngularJS Custom Directive sample not working

分類Dev

Calling a parent directive's function in a child directive

分類Dev

ajax call inside ajax success jquery then ajaxstart function not working

分類Dev

JQuery .ajax() function call not working, but success method runs

分類Dev

JQuery code working properly in jsfiddle but not in browser even with $(window).load(function())

分類Dev

Typescript angular directive controller function

分類Dev

VueJS Custom Directive Function as Argument

分類Dev

Function not working

分類Dev

jquery .not() and :not() are NOT working

分類Dev

Accessing function on scope in directive link function

分類Dev

Angularjs Custom Directive ng-click not working

分類Dev

Angular.js directive isolated scope not working

分類Dev

AngularJS: include not working from a custom directive

分類Dev

Angular.js directive isolated scope not working

分類Dev

Angularjs directive with bi-directional binding not working

Related 関連記事

  1. 1

    Google Maps for Angular directive not working in function

  2. 2

    jQuery .post() function not working

  3. 3

    jQuery click(function) not working

  4. 4

    click function not working in JQuery

  5. 5

    jQuery width function parameters not working

  6. 6

    Reason for jQuery Cycle function not working

  7. 7

    jQuery function working with more data

  8. 8

    jquery click function not working for me

  9. 9

    $.ajax is not a function, but the jquery library is working

  10. 10

    custom directive not working

  11. 11

    Jquery function on Dynamically created List not working

  12. 12

    jQuery function .split() not working, AJAX data not submitted

  13. 13

    jQuery find className not working for second click function

  14. 14

    RegEx not working in directive - Angular 4

  15. 15

    AngularJS Custom Directive sample not working

  16. 16

    Calling a parent directive's function in a child directive

  17. 17

    ajax call inside ajax success jquery then ajaxstart function not working

  18. 18

    JQuery .ajax() function call not working, but success method runs

  19. 19

    JQuery code working properly in jsfiddle but not in browser even with $(window).load(function())

  20. 20

    Typescript angular directive controller function

  21. 21

    VueJS Custom Directive Function as Argument

  22. 22

    Function not working

  23. 23

    jquery .not() and :not() are NOT working

  24. 24

    Accessing function on scope in directive link function

  25. 25

    Angularjs Custom Directive ng-click not working

  26. 26

    Angular.js directive isolated scope not working

  27. 27

    AngularJS: include not working from a custom directive

  28. 28

    Angular.js directive isolated scope not working

  29. 29

    Angularjs directive with bi-directional binding not working

ホットタグ

アーカイブ