在AngularJS中使用模態框需要引用的文件:
需要注意版本要一致,高版本的不支持這種方法,會出錯
將需要彈出的模態框的內容寫在 script 標簽中,指明屬性,放在頁面中
<script type="text/ng-template" id="modal.html"> <div> <div class="modal-header"> <h3 class="modal-title" align="center"> 標題信息 </h3> </div> <div class="modal-body"> <div align="center"> 模態框內容 </div> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()"> 確認 </button> <button class="btn btn-warning" ng-click="cancel()"> 退出 </button> </div></div></script>
在App和Controller中注入模態框
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標簽中定義的id controller : 'modalCtrl',//modal對應的Controller resolve : { data : function() {//data作為modal的controller傳入的參數 return data;//用于傳遞數據 } } }) }}//模態框對應的Controllerapp.controller('modalCtrl', function($scope, $modalInstance, data) { $scope.data= data; //在這里處理要進行的操作 $scope.ok = function() { $modalInstance.close(); }; $scope.cancel = function() { $modalInstance.dismiss('cancel'); }});
添加事件觸發顯示模態框
<button ng-click="openModal()">打開模態框</button>
html
<!DOCTYPE html><html ng-app="app" ng-controller="modalController"><head> <title>ng-model模態框</title></head><link rel="external nofollow" rel="stylesheet"><body><button ng-click="openModal()">打開模態框</button><script type="text/ng-template" id="modal.html"> <div> <div class="modal-header"> <h3 class="modal-title" align="center"> 標題信息 </h3> </div> <div class="modal-body"> <div align="center"> 模態框內容 <br> {{data}} </div> </div> <div class="modal-footer"> <button class="btn btn-primary" ng-click="ok()"> 確認 </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傳遞的數據"; $scope.openModal = function() { var modalInstance = $modal.open({ templateUrl : 'modal.html',//script標簽中定義的id controller : 'modalCtrl',//modal對應的Controller resolve : { data : function() {//data作為modal的controller傳入的參數 return data;//用于傳遞數據 } } }) } }) //模態框對應的Controller app.controller('modalCtrl', function($scope, $modalInstance, data) { $scope.data= data; //在這里處理要進行的操作 $scope.ok = function() { $modalInstance.close(); }; $scope.cancel = function() { $modalInstance.dismiss('cancel'); } });</script></body></html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答