sapui5 - SAPUI5:视图/页面上的按键
问题描述
在我的 sapui5 应用程序中,我的 Home 控制器中有以下代码
onAfterRendering: function() {
var s = this;
document.addEventListener("keypress", function onPress(event) {
if ( event.keyCode === 50) {
alert("Test");
}
});
因此,当我按下键时,由于元素“文档”,消息将显示在主视图和其他视图中
但我希望在每个视图/控制器中都有一个其他功能或消息到相同的键码。
那么如何附加到特定视图?我不想将它附加到控件上。
解决方案
每次访问视图时,您都必须删除并添加事件监听器。
视图1:
onInit: function(){
this.getRouter().getRoute("myView1").attachMatched(this._onRouteMatched, this); //Call _onRouteMatched on every visit from myView1
},
_onRouteMatched: function (oEvent) {
document.removeEventListener("keypress"); //Remove the event listener
document.addEventListener("keypress", function onPress(event) { //Add the event listener again with another message
if ( event.keyCode === 50) {
alert("Test View 1");
}
}
视图2:
onInit: function(){
this.getRouter().getRoute("myView2").attachMatched(this._onRouteMatched, this);
},
_onRouteMatched: function (oEvent) {
document.removeEventListener("keypress");
document.addEventListener("keypress", function onPress(event) {
if ( event.keyCode === 50) {
alert("Test View 2");
}
}
推荐阅读
- java - 使用 Maven 执行 Cucumber 测试
- python - 用python计算csv文件中的行数问题
- c# - 事件 ControlRemoved 事件后如何获取刷新的 TabPages 列表?
- php - 在原始类中调用静态方法
- c++ - 有人可以帮我并行化这个 C++ 代码吗?
- apache-spark - 使用 Kafka 使用 Spark 结构化批处理作业管理偏移量
- python - 基于传入路径更新字典中嵌套值的简单方法
- python - 如何同时运行两个 for 循环并决定输出
- amazon-web-services - boto3 list_objects_v2 StartAfter 无法正常工作
- testing - 获取赛普拉斯数组中不包含值的第一个元素