$modal只有一个方法:open,该方法的属性有:
-
templateUrl
:模态窗口的地址 -
template:用于显示html标签
-
scope
:一个作用域为模态的内容使用(事实上,$modal会创建一个当前作用域的子作用域
)默认为$rootScope
-
controller
:为$modal指定的控制器,初始化$scope,该控制器可用$modalInstance
注入 -
resolve
:定义一个成员并将他传递给$modal指定的控制器,相当于routes的一个reslove属性,如果需要传递一个objec对象,需要使用angular.copy() -
backdrop
:控制背景,允许的值:true(默认),false(无背景),“static
” - 背景是存在的,但点击模态窗口之外时,模态窗口不关闭 -
keyboard
:当按下Esc时,模态对话框是否关闭,默认为ture -
windowClass
:指定一个class并被添加到模态窗口中
open方法返回一个实例,该实例具有如下属性:
-
close(result)
:关闭模态窗口并传递一个结果 -
dismiss(reason)
:撤销模态方法并传递一个原因 -
result
:一个契约,当模态窗口被关闭或撤销时传递 -
opened
:一个契约,当模态窗口打开并且加载完内容时传递的变量
另外,$modalInstance扩展了两个方法$close(result)
、$dismiss(reason)
,这些方法很容易关闭窗口并且不需要额外的控制器
1 <!DOCTYPE html> 2 <html ng-app="ModalDemo"> 3 <head> 4 <title></title> 5 <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet"> 6 <script src="lib/angular/angular.min.js"></script> 7 <script src="lib/bootstrap-gh-pages/ui-bootstrap-tpls-0.7.0.min.js"></script> 8 <script src="lib/angular/i18n/angular-locale_zh-cn.js"></script> 9 </head> 10 <body> 11 <div ng-controller="ModalDemoCtrl"> 12 <script type="text/ng-template" id="myModalContent.html" /> 13 <div class="modal-header"> 14 <h3>I'm a modal!</h3> 15 </div> 16 <div class="modal-body"> 17 <ul> 18 <li ng-repeat="item in items"><a 19 ng-click="selected.item = item">{{ item }}</a></li> 20 </ul> 21 Selected: <b>{{ selected.item }}</b> 22 </div> 23 <div class="modal-footer"> 24 <button class="btn btn-primary" ng-click="ok()">OK</button> 25 <button class="btn btn-warning" ng-click="cancel()">Cancel</button> 26 </div> 27 </script> 28 <button class="btn" ng-click="open()">Open me!</button> 29 </div> 30 <script> 31 var ModalDemo = angular.module('ModalDemo', [ 'ui.bootstrap' ]); 32 var ModalDemoCtrl = function($scope, $modal, $log) { 33 $scope.items = [ 'item1', 'item2', 'item3' ]; 34 $scope.open = function() { 35 var modalInstance = $modal.open({ 36 templateUrl : 'myModalContent.html', 37 controller : ModalInstanceCtrl, 38 resolve : { 39 items : function() { 40 return $scope.items; 41 } 42 } 43 }); 44 modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数 45 console.log('modal is opened'); 46 }); 47 modalInstance.result.then(function(result) { 48 console.log(result); 49 }, function(reason) { 50 console.log(reason);// 点击空白区域,总会输出backdrop 51 // click,点击取消,则会暑促cancel 52 $log.info('Modal dismissed at: ' + new Date()); 53 }); 54 }; 55 }; 56 var ModalInstanceCtrl = function($scope, $modalInstance, items) { 57 $scope.items = items; 58 $scope.selected = { 59 item : $scope.items[0] 60 }; 61 $scope.ok = function() { 62 $modalInstance.close($scope.selected); 63 }; 64 $scope.cancel = function() { 65 $modalInstance.dismiss('cancel'); 66 }; 67 }; 68 </script> 69 </body> 70 </html>