Skip to content
Advertisement

How to switch from graph view and tableview with a toggle button in Ionic 6/Angular?

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:

JavaScript

The default view is the graphview via chart.js. Both components works fine. This is my code to hide and show:

JavaScript

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.

JavaScript hide/show element

How can I switch between the components and show only view?

JSFiddle link: https://jsfiddle.net/f8a3wgxb/

Advertisement

Answer

An example based off @alex87’s comment:

JavaScript

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 *ngIfElsehttps://angular.io/api/common/NgIf

Note: I tend to go overboard in my tag wrapping, thus the ng-containers, but *ngIf is typically available on almost all tags, as is hidden – 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.

JavaScript

Reduced to only two lines.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement