【转】AngularJs 弹出框 model(模态框)

时间:2015-05-05 11:55:06   收藏:0   阅读:24346

原文转至 http://blog.csdn.net/violet_day/article/details/17170585 

$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们

$modal仅有一个方法open(options)

open方法返回一个模态实例,该实例有如下属性

另外,$modalInstance扩展了两个方法$close(result)$dismiss(reason),这些方法很容易关闭窗口并且不需要额外的控制器

HTML 

 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>Im a modal!</h3>  
15         </div>  
16         <div class="modal-body">  
17             <ul>  
18                 <li ng-repeat="item in items">  
19                     <a ng-click="selected.item = item">{{ item }}</a>  
20                 </li>  
21             </ul>  
22             Selected: <b>{{ selected.item }}</b>  
23         </div>  
24         <div class="modal-footer">  
25             <button class="btn btn-primary" ng-click="ok()">OK</button>  
26             <button class="btn btn-warning" ng-click="cancel()">Cancel</button>  
27         </div>  
28     </script>  
29     <button class="btn" ng-click="open()">Open me!</button>  
30 </div>  
31 <script>  
32     var ModalDemo = angular.module(ModalDemo, [ui.bootstrap]);  
33     var ModalDemoCtrl = function ($scope, $modal, $log) {  
34         $scope.items = [item1, item2, item3];  
35         $scope.open = function () {  
36             var modalInstance = $modal.open({  
37                 templateUrl: myModalContent.html,  
38                 controller: ModalInstanceCtrl,  
39                 resolve: {  
40                     items: function () {  
41                         return $scope.items;  
42                     }  
43                 }  
44             });  
45             modalInstance.opened.then(function(){//模态窗口打开之后执行的函数  
46                 console.log(modal is opened);  
47             });  
48             modalInstance.result.then(function (result) {  
49                  console.log(result);  
50             }, function (reason) {  
51                 console.log(reason);//点击空白区域,总会输出backdrop 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>  

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!