angularjs 弹出框 $modal传值
生活随笔
收集整理的這篇文章主要介紹了
angularjs 弹出框 $modal传值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
$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),這些方法很容易關閉窗口并且不需要額外的控制器。
<!DOCTYPE html> <html ng-app="ModalDemo"> <head> <title></title> <link href="lib/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <script src="lib/angular/angular.min.js"></script> <script src="lib/bootstrap-gh-pages/ui-bootstrap-tpls-0.7.0.min.js"></script> <script src="lib/angular/i18n/angular-locale_zh-cn.js"></script> </head> <body> <div ng-controller="ModalDemoCtrl"> <button class="btn" ng-click="open()">Open me!</button> <!-- start:以下頁面可單獨保存到一個HTML頁面中 --><script type="text/ng-template" id="myModalContent.html" /> <div class="modal-header"> <h3>I'm a modal!</h3> </div> <div class="modal-body"> <ul> <li ng-repeat="item in items"><a ng-click="selected.item = item">{{ item }}</a></li> </ul> Selected: <b>{{ selected.item }}</b> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()">OK</button> <button class="btn btn-warning" ng-click="cancel()">Cancel</button> </div> <!-- 也可以使用ng-include引入其他頁面:<ng-include src="'<%=request.getContextPath() %>/index/jsp/tpl/smoke/scenario_add.jsp'"></ng-include> --></script> <!-- end --></div> <script> var ModalDemo = angular.module('ModalDemo', [ 'ui.bootstrap' ]); var ModalDemoCtrl = function($scope, $modal, $log) { $scope.items = [ 'item1', 'item2', 'item3' ]; $scope.open = function() { var modalInstance = $modal.open({ templateUrl : 'myModalContent.html', controller : ModalInstanceCtrl, resolve : { items : function() { return $scope.items; } } }); modalInstance.opened.then(function() {// 模態窗口打開之后執行的函數 console.log('modal is opened'); }); modalInstance.result.then(function(result) { console.log(result); //接收模態框回傳的值}, function(reason) { console.log(reason);// 點擊空白區域,總會輸出backdrop // click,點擊取消,則會觸發cancel $log.info('Modal dismissed at: ' + new Date()); }); }; };//傳遞給模態框的controller,該函數中的方法只能在模態框中使用var ModalInstanceCtrl = function($scope, $modalInstance, items) { $scope.items = items; $scope.selected = { item : $scope.items[0] }; $scope.ok = function() { //模態框中的ok點擊操作$modalInstance.close($scope.selected); }; $scope.cancel = function() { //點擊模態框周邊的空白區域關閉時,會回傳日志$modalInstance.dismiss('cancel'); }; }; </script> </body> </html>?
轉載于:https://my.oschina.net/u/2391658/blog/904418
總結
以上是生活随笔為你收集整理的angularjs 弹出框 $modal传值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在 Linux 上使用 Meld 比较文
- 下一篇: hdu1068 Girls and Bo