ng-click stops working after the first use of $compile when using nested directives

Scott Sword

I have an Angular modal directive that uses a helper/wrapper directive. This way I can always use the same wrapper and just load a different template where needed for different modal content.

PROBLEM: This snippet works, but only for the first life cycle of the modal. So I can fire the modal, close the modal and fire it again. But once the modal is open the second time none of the ng-click directives work. Any tips would be just super.

Usage

<button my-modal="views/login.html">Launch Login-specific Modal</button>

Directive Module (app.js)

angular.module('myModal',[])

.directive('modalWrapper', function(){
  return {
    replace: true,
    templateUrl: 'views/modal.html',
    controller: function($scope, $element){
      $scope.close = function(){
        $element.remove();
      };
      // NOTE: I use this array to showcase that ng-repeat still works the second time although ng-click stops functioning properly.
      $scope.others = ["One", "Two", "Three"];
    }
  }
})

.directive('myModal', function( $compile){

      function link(scope, element, attr){

      scope.partial = attr.myModal; // NOTE: Loads sub template via ng-include

      var ngModal = $compile('<div modal-wrapper></div>')(scope);

      element.on('click', function(){
        angular.element('body').append(ngModal);
      });

      scope.yo = function(){
        alert("Yo from inside template.");
      };

    }

    return {
      link: link,
      scope: {}
    }
});

Templates

modal.html

<div class="my-modal">
    <p>Modal Wrapper</p>
    <div ng-include="partial"></div>
  <button ng-click="close()">Close</button>
  <p>This just proves that other directives still work (ng-repeat), but ng-click does not.</p>
  <div ng-repeat="stuff in others">
    <p>{{stuff}}</p>
  </div>
</div>

login.html

<h1>Well hey there, I'm the login template.</h1>
<button ng-click="yo()">Say Yo</button>
Andyrooger

I think the problem is that you are destroying the scope on which the ng-click is compiled.

When scope.close() is called, an $element.remove() occurs. This both removes the element from the DOM, and destroys the scope to which it is attached. This will result in your ng-click being de-registered.

Unfortunately (as of last time I checked), element.detach() also destroys scope, so your best bet is to compile and append the element to body only once. After this you can use element.show() and element.hide() to show and hide the modal. Alternatively you can recompile the modal each time you want to show it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ng-click not working after first click (embedded directives)

From Dev

ng-click not working after first click (embedded directives)

From Dev

ng-click stops working when using ng-if

From Dev

ng-click stops working after first click, but only in later angular versions

From Dev

Jquery (2.2.0) click listener stops working after few clicks when using .on() but .click() works fine

From Dev

ng-click not working after compile ng-bind-html

From Dev

AngularJS: ng-click still not working after using "$compile(html)($scope)"

From Dev

ng-click not firing in nested directives

From Dev

ng-Click not working in angularjs after clicking for the first time

From Dev

Javascript - Code stops working after click on button

From Dev

Bxslider Plugin stops working after first "action"

From Dev

Intersect function stops working after the first intersect

From Dev

Bxslider Plugin stops working after first "action"

From Dev

Submit stops working after first successful submit

From Dev

ng-click increase limit only works once and then stops working after adding one row to ng-repeat

From Dev

Angular ng-click not working with $compile

From Dev

Ng-Click is not working in directive with Compile

From Dev

ng-class button stops working after using its activator with a link

From Dev

Trouble with Angular Nested Directives when using ControllerAs

From Dev

AngularJS directives: ng-click is not triggered after blur

From Dev

Angular stops working When i use ng-controller in the html tag

From Dev

EXE stops working after compile. (Mysql C++ Connector)

From Dev

I am using Angular js1 .When i use angular select box ng-click is not working

From Dev

ionic scroll stops working after some using

From Dev

ionic scroll stops working after some using

From Dev

JQuery not working on first click, but working after it

From Dev

Internet stops working when using static IP

From Dev

Wireless stops working soon after computer start when using WPA2 encryption

From Dev

SpeechSynthesis stops working after first utterance in FireFox, but works in Chrome

Related Related

  1. 1

    ng-click not working after first click (embedded directives)

  2. 2

    ng-click not working after first click (embedded directives)

  3. 3

    ng-click stops working when using ng-if

  4. 4

    ng-click stops working after first click, but only in later angular versions

  5. 5

    Jquery (2.2.0) click listener stops working after few clicks when using .on() but .click() works fine

  6. 6

    ng-click not working after compile ng-bind-html

  7. 7

    AngularJS: ng-click still not working after using "$compile(html)($scope)"

  8. 8

    ng-click not firing in nested directives

  9. 9

    ng-Click not working in angularjs after clicking for the first time

  10. 10

    Javascript - Code stops working after click on button

  11. 11

    Bxslider Plugin stops working after first "action"

  12. 12

    Intersect function stops working after the first intersect

  13. 13

    Bxslider Plugin stops working after first "action"

  14. 14

    Submit stops working after first successful submit

  15. 15

    ng-click increase limit only works once and then stops working after adding one row to ng-repeat

  16. 16

    Angular ng-click not working with $compile

  17. 17

    Ng-Click is not working in directive with Compile

  18. 18

    ng-class button stops working after using its activator with a link

  19. 19

    Trouble with Angular Nested Directives when using ControllerAs

  20. 20

    AngularJS directives: ng-click is not triggered after blur

  21. 21

    Angular stops working When i use ng-controller in the html tag

  22. 22

    EXE stops working after compile. (Mysql C++ Connector)

  23. 23

    I am using Angular js1 .When i use angular select box ng-click is not working

  24. 24

    ionic scroll stops working after some using

  25. 25

    ionic scroll stops working after some using

  26. 26

    JQuery not working on first click, but working after it

  27. 27

    Internet stops working when using static IP

  28. 28

    Wireless stops working soon after computer start when using WPA2 encryption

  29. 29

    SpeechSynthesis stops working after first utterance in FireFox, but works in Chrome

HotTag

Archive