AngularJs 弹出模态框(model)
推荐阅读:详解AngularJS 模态对话框
$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们。
$modal仅有一个方法open(options)
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),这些方法很容易关闭窗口并且不需要额外的控制器
HTML
<!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-...min.js"></script> <script src="lib/angular/in/angular-locale_zh-cn.js"></script> </head> <body> <div ng-controller="ModalDemoCtrl"> <script type="text/ng-template" id="myModalContent.html"> <div class="modal-header"> <h>I'm a modal!</h> </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> </script> <button class="btn" ng-click="open()">Open me!</button> </div> <script> var ModalDemo = angular.module('ModalDemo', ['ui.bootstrap']); var ModalDemoCtrl = function ($scope, $modal, $log) { $scope.items = ['item', 'item', 'item']; $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()); }); }; }; var ModalInstanceCtrl = function ($scope, $modalInstance, items) { $scope.items = items; $scope.selected = { item: $scope.items[] }; $scope.ok = function () { $modalInstance.close($scope.selected); }; $scope.cancel = function () { $modalInstance.dismiss('cancel'); }; }; </script> </body> </html>
以上所述是小编给大家介绍的AngularJs 弹出模态框(model)的相关内容,希望对大家有所帮助!
栏 目:AngularJS
本文地址:http://www.codeinn.net/misctech/98616.html