AngularJs 彈出模態(tài)框(model)
推薦閱讀:詳解AngularJS 模態(tài)對(duì)話框
$modal是一個(gè)可以迅速創(chuàng)建模態(tài)窗口的服務(wù),創(chuàng)建部分頁,控制器,并關(guān)聯(lián)他們。
$modal僅有一個(gè)方法open(options)
templateUrl:模態(tài)窗口的地址
template:用于顯示html標(biāo)簽
scope:一個(gè)作用域?yàn)槟B(tài)的內(nèi)容使用(事實(shí)上,$modal會(huì)創(chuàng)建一個(gè)當(dāng)前作用域的子作用域)默認(rèn)為$rootScope
controller:為$modal指定的控制器,初始化$scope,該控制器可用$modalInstance注入
resolve:定義一個(gè)成員并將他傳遞給$modal指定的控制器,相當(dāng)于routes的一個(gè)reslove屬性,如果需要傳遞一個(gè)objec對(duì)象,需要使用angular.copy()
backdrop:控制背景,允許的值:true(默認(rèn)),false(無背景),“static” - 背景是存在的,但點(diǎn)擊模態(tài)窗口之外時(shí),模態(tài)窗口不關(guān)閉
keyboard:當(dāng)按下Esc時(shí),模態(tài)對(duì)話框是否關(guān)閉,默認(rèn)為ture
windowClass:指定一個(gè)class并被添加到模態(tài)窗口中
open方法返回一個(gè)模態(tài)實(shí)例,該實(shí)例有如下屬性
close(result):關(guān)閉模態(tài)窗口并傳遞一個(gè)結(jié)果
dismiss(reason):撤銷模態(tài)方法并傳遞一個(gè)原因
result:一個(gè)契約,當(dāng)模態(tài)窗口被關(guān)閉或撤銷時(shí)傳遞
opened:一個(gè)契約,當(dāng)模態(tài)窗口打開并且加載完內(nèi)容時(shí)傳遞的變量
另外,$modalInstance擴(kuò)展了兩個(gè)方法$close(result)、$dismiss(reason),這些方法很容易關(guān)閉窗口并且不需要額外的控制器
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(){//模態(tài)窗口打開之后執(zhí)行的函數(shù)
console.log('modal is opened');
});
modalInstance.result.then(function (result) {
console.log(result);
}, function (reason) {
console.log(reason);//點(diǎn)擊空白區(qū)域,總會(huì)輸出backdrop click,點(diǎn)擊取消,則會(huì)暑促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 彈出模態(tài)框(model)的相關(guān)內(nèi)容,希望對(duì)大家有所幫助!
相關(guān)文章
Angular-UI Bootstrap組件實(shí)現(xiàn)警報(bào)功能
這篇文章主要介紹了Angular-UI Bootstrap組件實(shí)現(xiàn)警報(bào)功能,對(duì)Angular.js services的學(xué)習(xí)有所幫助,需要的朋友可以參考下2018-07-07
簡介可以自動(dòng)完成UI的AngularJS工具angular-smarty
這篇文章主要介紹了簡介可以自動(dòng)完成UI的AngularJS工具angular-smarty,包括其中隔離作用域綁定指令符和promise的使用,需要的朋友可以參考下2015-06-06
解決Angularjs異步操作后臺(tái)請(qǐng)求用$q.all排列先后順序問題
解決Angularjs異步操作后臺(tái)請(qǐng)求用$q.all排列先后順序問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Angular 2應(yīng)用的8個(gè)主要構(gòu)造塊有哪些
這篇文章主要為大家詳細(xì)介紹了Angular 2應(yīng)用的8個(gè)主要構(gòu)造塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
angular報(bào)錯(cuò)can't resolve all parameters&nb
這篇文章主要介紹了angular報(bào)錯(cuò)can't resolve all parameters for []的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的select二級(jí)聯(lián)動(dòng)下拉菜單功能,結(jié)合完整實(shí)例形式分析了AngularJS實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10

