javascript - angularjs custom twitter bootstrap modal directive -
i trying create custom twitter bootstrap modal popup using angularjs directives. problem how can control popup controller
<!-- modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> <h4 class="modal-title">hello modal</h4> </div> <div class="modal-body"> <p>modal popup</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div> </div>
and controller directive is
var modal = angular.module('directivemodal', ['ngroute','nganimate']); modal.directive('loginmodal', function() { return { restrict: 'ea', templateurl: 'directivetemplate/modal.html', link: function(scope,elem,attr) { elem.bind("click",function() { console.log("opening modal"); }); } } });
and how have called directive init page
var app = angular.module('myapp', ['ngroute','nganimate','directivemodal']); app.config(function($routeprovider) { $routeprovider .when("/",{ templateurl : "template/landing.html", controller : "landingctrl" }) .when("/home",{ templateurl : "template/home.html", controller : "homectrl" }) .when("/post",{ templateurl : "template/post.html", controller : "postctrl" }) .otherwise( { redirectto: '/' }); });
how can control modal popup in html have this.
<div login-modal></div>
i customize modal need. if want have control text display. add new elements , call popup/show on conditions met controllers.
basically inside directive need use standard method bootstrap. like:
link: function postlink(scope, element, attrs) { scope.title = attrs.title; scope.$watch(attrs.visible, function(value){ if(value == true) $(element).modal('show'); else $(element).modal('hide'); }); $(element).on('shown.bs.modal', function(){ scope.$apply(function(){ scope.$parent[attrs.visible] = true; }); }); $(element).on('hidden.bs.modal', function(){ scope.$apply(function(){ scope.$parent[attrs.visible] = false; }); });
as can see inside $watch function have bootstrap method. original answer copied solution available here: simple angular directive bootstrap modal
Comments
Post a Comment