$modal只有一個方法:open,該方法的屬性有: css
templateUrl
:模態窗口的地址html
template:用於顯示html標籤
angularjs
scope
:一個做用域爲模態的內容使用(事實上,$modal會建立一個當前做用域的子做用域
)默認爲$rootScope
bootstrap
controller
:爲$modal指定的控制器,初始化$scope,該控制器可用$modalInstance
注入app
resolve
:定義一個成員並將他傳遞給$modal指定的控制器,至關於routes的一個reslove屬性,若是須要傳遞一個objec對象,須要使用angular.copy()函數
backdrop
:控制背景,容許的值:true(默認),false(無背景),「static
」 - 背景是存在的,但點擊模態窗口以外時,模態窗口不關閉post
keyboard
:當按下Esc時,模態對話框是否關閉,默認爲tureui
windowClass
:指定一個class並被添加到模態窗口中url
open方法返回一個實例,該實例具備以下屬性:spa
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>