i have div always show on desktop view i want the same div show and hide depend on button click in mobile view angular
Assistant
To achieve this, you can use Angular's ngClass directive to conditionally apply a CSS class to your div based on a button click. Here's an example code:
First, add a boolean variable showDiv to your component's TypeScript file (component.ts), and set its initial value to false:
export class YourComponent {
showDiv: boolean = false;
// Rest of your component code
}
Then, in your component's HTML file (component.html), you can conditionally apply a CSS class based on the value of showDiv using ngClass:
Next, add a function to your component's TypeScript file to toggle the value of when the button is clicked:
User
but here the div didn't display in desktop view
Assistant
Apologies for the confusion. If you want the div to always show in desktop view and toggle based on button clicks in mobile view, you can modify the code as follows:
First, update your component's TypeScript file () to include a variable to track the current view:
toggleDiv()
showDiv
export class YourComponent {
showDiv: boolean = false;
toggleDiv() {
this.showDiv = !this.showDiv;
}
// Rest of your component code
}
Finally, define the CSS class .show-div in your component's CSS file (component.css) to control the visibility of the div in mobile view:
@media screen and (max-width: 767px) {
.show-div {
display: block; /* or any other CSS to show the div in mobile view */
}
}
Here, max-width: 767px is just an example, you can adjust it to match your mobile view breakpoint.
Now, when the button is clicked, the value of showDiv will be toggled, and the div will be shown or hidden based on the CSS class applied to it, only in mobile view.
component.ts
isMobileView
export class YourComponent {
isMobileView: boolean = false;
showDiv: boolean = !this.isMobileView; // Show div by default in desktop view
// Rest of your component code
}
Next, add a function checkView() to your component's TypeScript file to detect if the current view is mobile or desktop based on the screen size:
export class YourComponent {
isMobileView: boolean = false;
showDiv: boolean = !this.isMobileView;
checkView() {
this.isMobileView = window.innerWidth <= 767; // Update the condition based on your desired breakpoint
this.showDiv = !this.isMobileView; // Update the div visibility based on the view
}
// Rest of your component code
}
Then, in your component's HTML file (component.html), update the div tag to show or hide based on the showDiv variable:
Lastly, add the following code snippet to your component's TypeScript file to toggle the div visibility when the button is clicked:
export class YourComponent {
isMobileView: boolean = false;
showDiv: boolean = !this.isMobileView;
checkView() {
this.isMobileView = window.innerWidth <= 767; // Update the condition based on your desired breakpoint
this.showDiv = !this.isMobileView; // Update the div visibility based on the view
}
toggleDiv() {
this.showDiv = !this.showDiv;
}
// Rest of your component code
ngOnInit() {
this.checkView(); // Detect the initial view on component initialization
window.addEventListener('resize', () => this.checkView()); // Update the view on window resize
}
}
Now, the div will always show in desktop view and toggle based on button clicks in mobile view. The checkView() function is used to detect the current view and update the showDiv variable accordingly. The div will have the show-div class applied to it when showDiv is true, triggering the CSS to display the div.