- Ionic 基础教程
- Ionic - 首页
- Ionic - 概述
- Ionic - 环境搭建
- Ionic CSS 组件
- Ionic - 颜色
- Ionic - 内容
- Ionic - 页眉
- Ionic - 页脚
- Ionic - 按钮
- Ionic - 列表
- Ionic - 卡片
- Ionic - 表单
- Ionic - 开关
- Ionic - 复选框
- Ionic - 单选按钮
- Ionic - 滑块
- Ionic - 选择框
- Ionic - 标签页
- Ionic - 网格
- Ionic - 图标
- Ionic - 内边距
- Ionic Javascript 组件
- Ionic - JS 动作面板
- Ionic - JS 背景遮罩
- Ionic - JS 内容
- Ionic - JS 表单
- Ionic - JS 事件
- Ionic - JS 页眉
- Ionic - JS 页脚
- Ionic - JS 键盘
- Ionic - JS 列表
- Ionic - JS 加载
- Ionic - JS 模态框
- Ionic - JS 导航
- Ionic - JS 弹出框
- Ionic - JS 弹出窗口
- Ionic - JS 滚动
- Ionic - JS 侧边菜单
- Ionic - JS 滑块
- Ionic - JS 标签页
- Ionic 高级概念
- Ionic - Cordova 集成
- Ionic - AdMob
- Ionic - 相机
- Ionic - Facebook
- Ionic - 应用内浏览器
- Ionic - 原生音频
- Ionic - 地理位置
- Ionic - 媒体
- Ionic - 启动画面
- Ionic 有用资源
- Ionic - 快速指南
- Ionic - 有用资源
- Ionic - 讨论
Ionic - Javascript 模态框
当激活 Ionic 模态框时,内容面板将显示在常规内容之上。模态框基本上是一个功能更强大的较大弹出窗口。默认情况下,模态框将覆盖整个屏幕,但可以根据需要进行优化。
使用模态框
在 Ionic 中实现模态框有两种方法。一种方法是添加单独的模板,另一种方法是在常规 HTML 文件内的 `<script>` 标签中添加它。首先,我们需要使用 Angular 依赖注入将我们的模态框连接到控制器。然后,我们需要创建一个模态框。我们将传入作用域并为模态框添加动画。
之后,我们将创建用于打开、关闭、销毁模态框的函数。最后两个函数放置在我们可以编写代码的地方,这些代码将在模态框隐藏或移除时触发。如果您不希望在模态框移除或隐藏时触发任何功能,可以删除最后两个函数。
控制器代码
.controller('MyController', function($scope, $ionicModal) {
$ionicModal.fromTemplateUrl('my-modal.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
//Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Execute action on hide modal
$scope.$on('modal.hidden', function() {
// Execute action
});
// Execute action on remove modal
$scope.$on('modal.removed', function() {
// Execute action
});
});
HTML 代码
<script id = "my-modal.html" type = "text/ng-template">
<ion-modal-view>
<ion-header-bar>
<h1 class = "title">Modal Title</h1>
</ion-header-bar>
<ion-content>
<button class = "button icon icon-left ion-ios-close-outline"
ng-click = "closeModal()">Close Modal</button>
</ion-content>
</ion-modal-view>
</script>
上一个示例中展示的方法是使用 `<script>` 标签作为模态框的容器,位于某个现有的 HTML 文件内。
第二种方法是在 `templates` 文件夹内创建一个新的模板文件。我们将使用与上一个示例相同的代码,但是我们将删除 `<script>` 标签,并且还需要更改控制器中的 `fromTemplateUrl` 以将模态框与新创建的模板连接。
控制器代码
.controller('MyController', function($scope, $ionicModal) {
$ionicModal.fromTemplateUrl('templates/modal-template.html', {
scope: $scope,
animation: 'slide-in-up',
}).then(function(modal) {
$scope.modal = modal;
});
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
//Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Execute action on hide modal
$scope.$on('modal.hidden', function() {
// Execute action
});
// Execute action on remove modal
$scope.$on('modal.removed', function() {
// Execute action
});
});
HTML 代码
<ion-modal-view>
<ion-header-bar>
<h1 class = "title">Modal Title</h1>
</ion-header-bar>
<ion-content>
<button class = "button icon icon-left ion-ios-close-outline"
ng-click = "closeModal()">Close Modal</button>
</ion-content>
</ion-modal-view>
使用 Ionic 模态框的第三种方法是内联插入 HTML。我们将使用 `fromTemplate` 函数而不是 `fromTemplateUrl`。
控制器代码
.controller('MyController', function($scope, $ionicModal) {
$scope.modal = $ionicModal.fromTemplate( '<ion-modal-view>' +
' <ion-header-bar>' +
'<h1 class = "title">Modal Title</h1>' +
'</ion-header-bar>' +
'<ion-content>'+
'<button class = "button icon icon-left ion-ios-close-outline"
ng-click = "closeModal()">Close Modal</button>' +
'</ion-content>' +
'</ion-modal-view>', {
scope: $scope,
animation: 'slide-in-up'
})
$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
//Cleanup the modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.modal.remove();
});
// Execute action on hide modal
$scope.$on('modal.hidden', function() {
// Execute action
});
// Execute action on remove modal
$scope.$on('modal.removed', function() {
// Execute action
});
});
所有三个示例都将具有相同的效果。我们将创建一个按钮来触发 `$ionicModal.show()` 以打开模态框。
HTML 代码
<button class = "button" ng-click = "openModal()"></button>
打开模态框时,它将包含一个用于关闭它的按钮。我们在 HTML 模板中创建了这个按钮。
还有其他模态框优化选项。我们已经展示了如何使用作用域和动画。下表显示了其他选项。
| 选项 | 类型 | 详情 |
|---|---|---|
| focusFirstInput | 布尔值 | 它将自动聚焦模态框的第一个输入。 |
| backdropClickToClose | 布尔值 | 点击背景遮罩时将启用关闭模态框。默认值为 true。 |
| hardwareBackButtonClose | 布尔值 | 点击硬件后退按钮时将启用关闭模态框。默认值为 true。 |
广告