如何使用Ionic 6/Angular中的切换按钮从图形视图和tableView切换?
我有两个要显示数据的组件。表视图和图形视图。两个组件都有相同的数据。我使用一个切换开关按钮在这些视图之间切换。这是我的代码:
<ion-header>
<ion-toolbar class="toolbar-color">
<ion-title>Views</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-grid>
<ion-row>
<ion-col>
<div class="ion-text-start">
<ion-item>
<ion-label>Switch to table view</ion-label>
<ion-toggle (click)="showTable()"></ion-toggle>
</ion-item>
</div>
</ion-col
</ion-row>
<ion-row>
<ion-col>
<app-tableview hidden="true" id="tableview"></app-tableview>
<canvas height="200" #lineCanvas></canvas>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
默认视图是通过Chart.js的GraphView。两个组件都可以正常工作。这是我要隐藏和显示的代码:
showTable() {
if (this.tableHidden === true) {
this.tableHidden = false;
document.getElementById("tableview").hidden = false;
} else if (this.tableHidden === false) {
this.tableHidden = true;
document.getElementById("tableview").hidden = false;
}
}
当我单击“切换开关”时,它不会隐藏图形视图,而是彼此显示表视图。我尝试了这篇文章的可见性,但行不通。
我如何在组件之间切换并仅显示视图?
jsfiddle链接: https://jsfiddle.net/f8a3wwgxb/
I have two components to show data. The table view and the graph view. Both components has the same data. I use a toggle switch button to switch between these views. This is my code:
<ion-header>
<ion-toolbar class="toolbar-color">
<ion-title>Views</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-grid>
<ion-row>
<ion-col>
<div class="ion-text-start">
<ion-item>
<ion-label>Switch to table view</ion-label>
<ion-toggle (click)="showTable()"></ion-toggle>
</ion-item>
</div>
</ion-col
</ion-row>
<ion-row>
<ion-col>
<app-tableview hidden="true" id="tableview"></app-tableview>
<canvas height="200" #lineCanvas></canvas>
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
The default view is the graphview via chart.js. Both components works fine. This is my code to hide and show:
showTable() {
if (this.tableHidden === true) {
this.tableHidden = false;
document.getElementById("tableview").hidden = false;
} else if (this.tableHidden === false) {
this.tableHidden = true;
document.getElementById("tableview").hidden = false;
}
}
When I click on the toggle switch it doesn't hide the graphview but it shows the table view among each other. I have tried the visibility from this post but doesn't work.
How can I switch between the components and show only view?
JSFiddle link: https://jsfiddle.net/f8a3wgxb/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个基于 @alex87的评论的示例:
不必是
ng-container
,但是仅角度的标签不会向DOM添加任何内容。您可以随时使用A或其他任何事情来做同样的事情...您也可以使用
*ngifelse
...将其整合起来。https://angular.io/api/api/common/common/ngif
注意:我倾向于过于舷外在我的标签包装中,因此
ng -container
s,但是*ngif
通常在几乎所有标签上都可用,hidden> hidden
- 用法。编辑:修复了隐藏的变量名称 - 我很懒惰,没有回头找到您所说的。
编辑#2:您可以整理并最小化隐藏变量的设置,而不是需要if/else。
减少到只有两条线。
An example based off @alex87's comment:
Doesn't have to be an
ng-container
, but that Angular-only tag will not add anything to the DOM. You can always use a or anything else to do just the same...You can also neaten it up by using an
*ngIfElse
...https://angular.io/api/common/NgIf
Note: I tend to go overboard in my tag wrapping, thus the
ng-container
s, but*ngIf
is typically available on almost all tags, as ishidden
- per your usage.Edit: Fixed the hidden variable name - I was lazy and didn't go back up to find what exactly you'd called it.
Edit #2: You can tidy up and minimise your setting of the hidden variable, instead of needing the if/else.
Reduced to only two lines.