AngularJS中使用ngModal模態(tài)框?qū)嵗?/h1>
更新時間:2017年05月27日 10:37:44 作者:嗚嗚嗚啦啦啦
本篇文章主要介紹了AngularJS中使用ngModal模態(tài)框?qū)嵗【幱X得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在AngularJS中使用模態(tài)框需要引用的文件:
- angular.js 1.5.5
- ui.bootstrap-tpls.js 0.11.2
- bootstrap.css 3.3.7
需要注意版本要一致,高版本的不支持這種方法,會出錯
將需要彈出的模態(tài)框的內(nèi)容寫在 script 標(biāo)簽中,指明屬性,放在頁面中
<script type="text/ng-template" id="modal.html">
<div>
<div class="modal-header">
<h3 class="modal-title" align="center">
標(biāo)題信息
</h3>
</div>
<div class="modal-body">
<div align="center">
模態(tài)框內(nèi)容
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">
確認(rèn)
</button>
<button class="btn btn-warning" ng-click="cancel()">
退出
</button>
</div>
</div>
</script>
在App和Controller中注入模態(tài)框
var app = angular.module('app', ['ui.bootstrap']);
app.controller('modalController', function($scope, $rootScope,$modal) {
$scope.openModel = function() {
var modalInstance = $modal.open({
templateUrl : 'modal.html',//script標(biāo)簽中定義的id
controller : 'modalCtrl',//modal對應(yīng)的Controller
resolve : {
data : function() {//data作為modal的controller傳入的參數(shù)
return data;//用于傳遞數(shù)據(jù)
}
}
})
}
}
//模態(tài)框?qū)?yīng)的Controller
app.controller('modalCtrl', function($scope, $modalInstance, data) {
$scope.data= data;
//在這里處理要進(jìn)行的操作
$scope.ok = function() {
$modalInstance.close();
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
}
});
添加事件觸發(fā)顯示模態(tài)框
<button ng-click="openModal()">打開模態(tài)框</button>
html
<!DOCTYPE html>
<html ng-app="app" ng-controller="modalController">
<head>
<title>ng-model模態(tài)框</title>
</head>
<link rel="external nofollow" rel="stylesheet">
<body>
<button ng-click="openModal()">打開模態(tài)框</button>
<script type="text/ng-template" id="modal.html">
<div>
<div class="modal-header">
<h3 class="modal-title" align="center">
標(biāo)題信息
</h3>
</div>
<div class="modal-body">
<div align="center">
模態(tài)框內(nèi)容 <br>
{{data}}
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">
確認(rèn)
</button>
<button class="btn btn-warning" ng-click="cancel()">
退出
</button>
</div>
</div>
</script>
<script src="https://cdn.bootcss.com/angular.js/1.5.5/angular.min.js"></script>
<script src="https://cdn.bootcss.com/angular-ui-bootstrap/0.11.2/ui-bootstrap-tpls.min.js"></script>
<script type="text/javascript">
var app = angular.module('app', ['ui.bootstrap']);
app.controller('modalController', function($scope, $rootScope, $modal) {
var data = "通過modal傳遞的數(shù)據(jù)";
$scope.openModal = function() {
var modalInstance = $modal.open({
templateUrl : 'modal.html',//script標(biāo)簽中定義的id
controller : 'modalCtrl',//modal對應(yīng)的Controller
resolve : {
data : function() {//data作為modal的controller傳入的參數(shù)
return data;//用于傳遞數(shù)據(jù)
}
}
})
}
})
//模態(tài)框?qū)?yīng)的Controller
app.controller('modalCtrl', function($scope, $modalInstance, data) {
$scope.data= data;
//在這里處理要進(jìn)行的操作
$scope.ok = function() {
$modalInstance.close();
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
}
});
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
淺談angular表單提交中ng-submit的默認(rèn)使用方法
今天小編就為大家分享一篇淺談angular表單提交中ng-submit的默認(rèn)使用方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 2018-09-09
-
AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能,涉及AngularJS事件響應(yīng)實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能的相關(guān)操作技巧,需要的朋友可以參考下 2018-01-01
-
Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解
結(jié)構(gòu)指令通過添加和刪除 DOM 元素來更改 DOM 布局。Angular 中兩個常見的結(jié)構(gòu)指令是 *ngIf 和 *ngFor,下面這篇文章主要給大家介紹了關(guān)于Angular 2.x結(jié)構(gòu)指令的相關(guān)資料,需要的朋友可以參考下。 2017-05-05
-
Angular限制input框輸入金額(是小數(shù)的話只保留兩位小數(shù)點(diǎn))
最近做項(xiàng)目遇到這樣的需求輸入框要求輸入金額,只能輸入數(shù)字,可以是小數(shù),必須保留小數(shù)點(diǎn)后兩位。下面分為兩部分代碼給大家介紹實(shí)現(xiàn)代碼,需要的的朋友參考下吧 2017-07-07
-
AngularJS實(shí)現(xiàn)的2048小游戲功能【附源碼下載】
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的2048小游戲,可實(shí)現(xiàn)通過鍵盤W、S、A、D鍵控制上下左右移動進(jìn)行游戲的功能,涉及AngularJS頁面元素動態(tài)操作及數(shù)值運(yùn)算等相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下 2018-01-01
-
詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式
數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機(jī)制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來操縱網(wǎng)站上存在的元素。 2021-05-05
-
詳解基于Bootstrap+angular的一個豆瓣電影app
本篇文章主要介紹了基于Bootstrap+angular的一個豆瓣電影app ,非常具有實(shí)用價值,需要的朋友可以參考下
2017-06-06
最新評論
在AngularJS中使用模態(tài)框需要引用的文件:
- angular.js 1.5.5
- ui.bootstrap-tpls.js 0.11.2
- bootstrap.css 3.3.7
需要注意版本要一致,高版本的不支持這種方法,會出錯
將需要彈出的模態(tài)框的內(nèi)容寫在 script 標(biāo)簽中,指明屬性,放在頁面中
<script type="text/ng-template" id="modal.html"> <div> <div class="modal-header"> <h3 class="modal-title" align="center"> 標(biāo)題信息 </h3> </div> <div class="modal-body"> <div align="center"> 模態(tài)框內(nèi)容 </div> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()"> 確認(rèn) </button> <button class="btn btn-warning" ng-click="cancel()"> 退出 </button> </div> </div> </script>
在App和Controller中注入模態(tài)框
var app = angular.module('app', ['ui.bootstrap']); app.controller('modalController', function($scope, $rootScope,$modal) { $scope.openModel = function() { var modalInstance = $modal.open({ templateUrl : 'modal.html',//script標(biāo)簽中定義的id controller : 'modalCtrl',//modal對應(yīng)的Controller resolve : { data : function() {//data作為modal的controller傳入的參數(shù) return data;//用于傳遞數(shù)據(jù) } } }) } } //模態(tài)框?qū)?yīng)的Controller app.controller('modalCtrl', function($scope, $modalInstance, data) { $scope.data= data; //在這里處理要進(jìn)行的操作 $scope.ok = function() { $modalInstance.close(); }; $scope.cancel = function() { $modalInstance.dismiss('cancel'); } });
添加事件觸發(fā)顯示模態(tài)框
<button ng-click="openModal()">打開模態(tài)框</button>
html
<!DOCTYPE html> <html ng-app="app" ng-controller="modalController"> <head> <title>ng-model模態(tài)框</title> </head> <link rel="external nofollow" rel="stylesheet"> <body> <button ng-click="openModal()">打開模態(tài)框</button> <script type="text/ng-template" id="modal.html"> <div> <div class="modal-header"> <h3 class="modal-title" align="center"> 標(biāo)題信息 </h3> </div> <div class="modal-body"> <div align="center"> 模態(tài)框內(nèi)容 <br> {{data}} </div> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()"> 確認(rèn) </button> <button class="btn btn-warning" ng-click="cancel()"> 退出 </button> </div> </div> </script> <script src="https://cdn.bootcss.com/angular.js/1.5.5/angular.min.js"></script> <script src="https://cdn.bootcss.com/angular-ui-bootstrap/0.11.2/ui-bootstrap-tpls.min.js"></script> <script type="text/javascript"> var app = angular.module('app', ['ui.bootstrap']); app.controller('modalController', function($scope, $rootScope, $modal) { var data = "通過modal傳遞的數(shù)據(jù)"; $scope.openModal = function() { var modalInstance = $modal.open({ templateUrl : 'modal.html',//script標(biāo)簽中定義的id controller : 'modalCtrl',//modal對應(yīng)的Controller resolve : { data : function() {//data作為modal的controller傳入的參數(shù) return data;//用于傳遞數(shù)據(jù) } } }) } }) //模態(tài)框?qū)?yīng)的Controller app.controller('modalCtrl', function($scope, $modalInstance, data) { $scope.data= data; //在這里處理要進(jìn)行的操作 $scope.ok = function() { $modalInstance.close(); }; $scope.cancel = function() { $modalInstance.dismiss('cancel'); } }); </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談angular表單提交中ng-submit的默認(rèn)使用方法
今天小編就為大家分享一篇淺談angular表單提交中ng-submit的默認(rèn)使用方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的錨點(diǎn)樓層跳轉(zhuǎn)功能,涉及AngularJS事件響應(yīng)實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)功能的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Angular 2.x學(xué)習(xí)教程之結(jié)構(gòu)指令詳解
結(jié)構(gòu)指令通過添加和刪除 DOM 元素來更改 DOM 布局。Angular 中兩個常見的結(jié)構(gòu)指令是 *ngIf 和 *ngFor,下面這篇文章主要給大家介紹了關(guān)于Angular 2.x結(jié)構(gòu)指令的相關(guān)資料,需要的朋友可以參考下。2017-05-05Angular限制input框輸入金額(是小數(shù)的話只保留兩位小數(shù)點(diǎn))
最近做項(xiàng)目遇到這樣的需求輸入框要求輸入金額,只能輸入數(shù)字,可以是小數(shù),必須保留小數(shù)點(diǎn)后兩位。下面分為兩部分代碼給大家介紹實(shí)現(xiàn)代碼,需要的的朋友參考下吧2017-07-07AngularJS實(shí)現(xiàn)的2048小游戲功能【附源碼下載】
這篇文章主要介紹了AngularJS實(shí)現(xiàn)的2048小游戲,可實(shí)現(xiàn)通過鍵盤W、S、A、D鍵控制上下左右移動進(jìn)行游戲的功能,涉及AngularJS頁面元素動態(tài)操作及數(shù)值運(yùn)算等相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01詳解Angular數(shù)據(jù)綁定及其實(shí)現(xiàn)方式
數(shù)據(jù)綁定是將應(yīng)用程序UI或用戶界面綁定到模型的機(jī)制。使用數(shù)據(jù)綁定,用戶將能夠使用瀏覽器來操縱網(wǎng)站上存在的元素。2021-05-05詳解基于Bootstrap+angular的一個豆瓣電影app
本篇文章主要介紹了基于Bootstrap+angular的一個豆瓣電影app ,非常具有實(shí)用價值,需要的朋友可以參考下2017-06-06