我是AngularJS的新手.我正在尝试实现可重用的模态Bootstrap.
这是index.html:
这是index.html:
<div ng-controller="mymodalcontroller"> <modal lolo="modal1" modal-body='body' modal-footer='footer' modal-header='header' data-ng-click="myRightButton()"></modal> <a href="#{{modal1}}" role="button" class="btn btn-success" data-toggle="modal">Launch Demo Modal</a> </div>
这是模块,控制器和指令:
var myModal = angular.module('myModal',[]); myModal.controller('mymodalcontroller',function ($scope) { $scope.header = 'Put here your header'; $scope.body = 'Put here your body'; $scope.footer = 'Put here your footer'; $scope.myRightButton = function (bool) { alert('!!! first function call!'); }; }); myModal.directive('modal',function () { return { restrict: 'EA',scope: { title: '=modalTitle',header: '=modalHeader',body: '=modalBody',footer: '=modalFooter',callbackbuttonleft: '&ngClickLeftButton',callbackbuttonright: '&ngClick',handler: '=lolo' },templateUrl: 'partialmodal.html',transclude: true,controller: function ($scope) { $scope.handler = 'pop'; },}; });
这是html模板:
<div id="{{handler}}" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> <h4 class="modal-title">{{header}}</h4> </div> <div class="modal-body"> <p class="text-warning">{{body}}</p> </div> <div class="modal-footer"> <p class="text-left">{{footer}}</p> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary" data-ng-click="callbackbuttonright(),$event.stopPropagation()">Save changes</button> </div> </div> </div> </div>
我希望“启动警报”按钮(在模态中)执行警报并且它做得很好.问题是,单击模态中的“取消”按钮并关闭窗口时会启动它.有任何想法吗?
这是工作代码:Code谢谢.
我建议你不要绑定到ng-click.它做了一些其他可以搞砸东西的神奇东西.部分中也存在语法错误.
我在这里解决了这些问题:
http://plnkr.co/edit/2jK2GFcKSiKgMQMynD1R?p=preview
总结一下:
的script.js:
将您的回调按钮绑定从ngClick更改为ngClickRightButton
myModal.directive('modal',callbackbuttonright: '&ngClickRightButton',}; });
index.html的:
更改数据-ng-单击以数据-ng-单击右键
<modal lolo="modal1" modal-body="body" modal-footer="footer" modal-header="header" data-ng-click-right-button="myRightButton()"></modal>
另一个小问题:
partialmodal.html:
改成 ;
<button type="button" class="btn btn-primary" data-ng-click="callbackbuttonright(); $event.stopPropagation()">Launch Alert</button>