I have a modular Angular directive that uses the helper / wrapper directive. That way, I can always use the same shell and just load another template where it is needed for other modal content.
PROBLEM: This snippet works, but only for the first modal life cycle. So I can start modal, close modal and run it again. But as soon as the modal function is opened a second time, none of the ng-click directives work. Any advice would be just super.
Using
<button my-modal="views/login.html">Launch Login-specific Modal</button>
Directory 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: {} } });
Patterns
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>
angularjs angularjs-directive
Swordfish0321
source share