- 离子基础教程
- 离子 - 主页
- 离子 - 概述
- Ionic - 环境设置
- Ionic CSS 组件
- 离子 - 颜色
- 离子 - 内容
- 离子 - 标头
- 离子 - 页脚
- 离子 - 按钮
- 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 - Cordova 集成
- 离子 - AdMob
- 离子 - 相机
- 离子 - Facebook
- Ionic - 在应用程序浏览器中
- Ionic - 原生音频
- 离子 - 地理定位
- 离子 - 媒体
- Ionic - 启动画面
- 离子有用资源
- 离子 - 快速指南
- 离子 - 有用的资源
- 离子 - 讨论
Ionic - Javascript 弹出窗口
该视图将出现在常规视图之上。
使用弹出窗口
可以使用ion-popover-view元素创建 Popover。应将此元素添加到 HTML 模板中,并且需要将$ionicPopover服务注入到控制器中。
添加弹出框的方法有3种。第一个是fromTemplate方法,它允许使用内联模板。添加弹出窗口的第二种和第三种方法是使用fromTemplateUrl方法。
让我们了解下面解释的fromtemplate方法。
Fromtemplate 方法的控制器代码
.controller('DashCtrl', function($scope, $ionicLoading, $ionicPopover) {
// .fromTemplate() method
var template = '<ion-popover-view>' + '<ion-header-bar>' +
'<h1 class = "title">Popover Title</h1>' +
'</ion-header-bar>'+ '<ion-content>' +
'Popover Content!' + '</ion-content>' + '</ion-popover-view>';
$scope.popover = $ionicPopover.fromTemplate(template, {
scope: $scope
});
$scope.openPopover = function($event) {
$scope.popover.show($event);
};
$scope.closePopover = function() {
$scope.popover.hide();
};
//Cleanup the popover when we're done with it!
$scope.$on('$destroy', function() {
$scope.popover.remove();
});
// Execute action on hide popover
$scope.$on('popover.hidden', function() {
// Execute action
});
// Execute action on remove popover
$scope.$on('popover.removed', function() {
// Execute action
});
})
如上所述,添加弹出窗口的第二种和第三种方法是使用fromTemplateUrl方法。除了fromTemplateUrl值之外,两种方式的控制器代码都是相同的。
如果将 HTML 添加到现有模板,则 URL 将为popover.html。如果我们想将 HTML 放入 templates 文件夹中,则 URL 将更改为templates/popover.html。
下面对这两个示例进行了解释。
fromTemplateUrl 的控制器代码
.controller('MyCtrl', function($scope, $ionicPopover) {
$ionicPopover.fromTemplateUrl('popover.html', {
scope: $scope
}).then(function(popover) {
$scope.popover = popover;
});
$scope.openPopover = function($event) {
$scope.popover.show($event);
};
$scope.closePopover = function() {
$scope.popover.hide();
};
//Cleanup the popover when we're done with it!
$scope.$on('$destroy', function() {
$scope.popover.remove();
});
// Execute action on hide popover
$scope.$on('popover.hidden', function() {
// Execute action
});
// Execute action on remove popover
$scope.$on('popover.removed', function() {
// Execute action
});
})
现在,我们将带有模板的脚本添加到 HTML 文件中,用于调用 popover 函数。
现有 HTML 文件中的 HTML 代码
<script id = "popover.html" type = "text/ng-template">
<ion-popover-view>
<ion-header-bar>
<h1 class = "title">Popover Title</h1>
</ion-header-bar>
<ion-content>
Popover Content!
</ion-content>
</ion-popover-view>
</script>
如果我们想创建一个 HTML 作为单独的文件,我们可以在templates文件夹中创建一个新的 HTML 文件,并使用与上述示例中使用的代码相同的代码,但不带脚本标签。
新创建的 HTML 文件如下。
<ion-popover-view>
<ion-header-bar>
<h1 class = "title">Popover Title</h1>
</ion-header-bar>
<ion-content>
Popover Content!
</ion-content>
</ion-popover-view>
我们需要的最后一件事是创建一个按钮,单击该按钮即可显示弹出窗口。
<button class = "button" ng-click = "openPopover($event)">Add Popover</button>
无论我们从上面的示例中选择什么方式,输出总是相同的。
下表显示了可以使用的$ionicPopover方法。
| 方法 | 选项 | 类型 | 细节 |
|---|---|---|---|
| 初始化(选项) | 范围、focusFirst、backgroundClickToClose、hardwareBackButtonClose | 对象、布尔值、布尔值、布尔值 | 范围用于将自定义范围传递给弹出窗口。默认为 $rootScope。focusFirstInput用于自动聚焦弹出窗口的第一个输入。backgroundClickToClose用于在单击背景时关闭弹出窗口。hardwareBackButtonClose用于在按下硬件后退按钮时关闭弹出窗口。 |
| 显示($事件) | $事件 | 承诺 | 弹出窗口显示完毕后已解决。 |
| 隐藏() | / | 承诺 | 弹出窗口完成隐藏后已解决。 |
| 消除() | / | 承诺 | 弹出窗口完成删除后即可解决。 |
| isShown() | / | 布尔值 | 如果显示弹出窗口则返回 true,否则返回 false。 |