Develop a toggle button feature that allows users to hide or reveal a specific div on

My goal is to implement a button with specific behavior:

1) Initially, the List View is displayed as default

2) When the user clicks on the button labeled Card View, the List View is hidden and the Card View is shown. The classes for these views are set as follows:

  • Class of Card View: button button-clear button-dark
  • Class of List View:
    button button-clear button-positive

3) Upon clicking the List View button, the Card View is hidden while the List View is displayed. The respective classes are adjusted accordingly:

  • Class of Card View:
    button button-clear button-positive
  • Class of List View: button button-clear button-dark

Although I have successfully achieved point 1), I am encountering difficulties in implementing points 2) and 3).

controller.js

function StatementsController($scope, $stateParams, DummyStatementsService) {
    $scope.active = true;
    $scope.toggle = function(view){
        if(view === 'list') {
            $scope.active = true;
        } else if(view === 'card') {
            $scope.active = false;
        }
    }
}

statements.view.html

<ion-content>
    <div class="row" style="margin-bottom: -1.35em" ng-controller="StatementsController">
        <div class="col">
            <button class="button button-clear button-dark" ng-click="toggle('list')">List View</button>
        </div>
        <div class="col col-right">
            <button class="button button-clear button-positive" ng-click="toggle('card')">Card View</button>
        </div>
    </div>

    <div ng-controller="StatementsController">
        <!-- List view -->
        <div ng-show="active">
            test 1
        </div>
        <!-- Card view -->
        <div ng-hide="active">
            test 2
        </div>
    </div>
</ion-content>

Answer №1

Instead of repeatedly adding either the button-dark or the button-positive class to the buttons, you can simplify it by doing the following:

Updated (x2)

<ion-content> 
    <div ng-controller="StatementsController"> <!-- move the ng-controller out here -->
        <div class="row" style="margin-bottom: -1.35em">
            <div class="col">
                <button class="button button-clear" 
                        ng-class="{'button-positive': active, 'button-dark': !active}" 
                        ng-click="toggle('list')">List View</button>
            </div>
            <div class="col col-right">
                <button class="button button-clear" 
                        ng-class="{'button-positive': !active, 'button-dark': active}" 
                        ng-click="toggle('card')">Card View</button>
            </div>
        </div>
        <div>
            <!-- List view -->
            <div ng-show="active">
                test 1
            </div>
            <!-- Card view -->
            <div ng-hide="active">
                test 2
            </div>
        </div>
    </div>
</ion-content>

Answer №2

Here is a sample code snippet:

controller:

function ViewController($scope, $stateParams, DummyService) {
    $scope.view = 'list';
}

html:

<ion-content controller="ViewController">
    <div class="row" style="margin-bottom: -1.35em" ng->
        <div class="col">
            <button class="button button-clear button-dark" ng-click="view = 'list'">List View</button>
        </div>
        <div class="col col-right">
            <button class="button button-clear button-positive" ng-click="view = 'card'">Card View</button>
        </div>
    </div>

    <div ng-controller="ViewController">
        <!-- List view -->
        <div ng-show="view === 'list">
            test 1
        </div>
        <!-- Card view -->
        <div ng-show="view === 'card'">
            test 2
        </div>
    </div>
</ion-content>

or, using an ng-switch:

<div ng-switch="view">
    <!-- List view -->
    <div ng-switch-when="list">
        test 1
    </div>
    <!-- Card view -->
    <div ng-switch-when="card">
        test 2
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to define the validity of an ng-model within a directive?

I am struggling with a directive to validate phone numbers in AngularJS. I want the ng-model to be considered invalid if the phone number is less than 10 digits. While trying out $setValidity, I encountered an issue where it says that it's not a funct ...

What are the steps to activate CORS in a MEAN STACK web application?

I am currently developing a MEAN-STACK application using the Mean-cli package of Node. One of the packages I am utilizing is the darksky weather API within the Information package. Additionally, I have 4 other custom packages located in the mean app's ...

Unusual Occurrence in Angular 2: Modifications in one tab affecting another

As a beginner in Angular 2, I am working on building a basic CRUD application with authentication. I have successfully created a simple login form that submits uid/password to a NodeJS backend through an Angular service using ngForm. I then display the aut ...

What is preventing me from using JavaScript to remove this class?

Struggling to implement a skeleton loading screen with CSS classes and JavaScript. The idea is to apply the 'skeleton' class to elements, style them accordingly, then remove the class using a timeout set in JavaScript. However, I'm encounter ...

"Modify hyperlink attributes to enhance security and optimize user experience

$("a[href*='youtube']").attr('rel', 'prettyPhoto'); Having some trouble targeting links on a page containing "youtube" in the URL. I'm trying to set their rel attribute to "prettyPhoto" so they open in a lightbox window. ...

Struggling to send data to Wufoo API using PHP and AJAX

I'm still getting the hang of PHP and attempting to send data to a Wufoo Form that includes the fields shown below: https://i.sstatic.net/yoOgy.png However, when trying to POST information to it, I keep receiving a 500: Internal Server Error along w ...

Attempting to showcase a collection of values, specifically focusing on highlighting the even numbers within the array

const sampleArray = [ 469, " " + 755, " " + 244, " " + 245, " " + 758, " " + 450, " " + 302, " " + 20, " " + 712, " " + 71, " " + 456, ...

Trouble with getTime() function functionality in Chrome browser

My JavaScript code is functioning properly in IE and Firefox. Take a look: var dt = new Date("17/05/2012 05:22:02").getTime(); However, when I try to run it in Chrome, the value of dt turns out to be NaN. I've been troubleshooting but can't see ...

Nodemailer is functioning properly in a local environment, however, it is encountering issues when

I am facing an issue with Nodemailer where it is sending emails successfully on my local machine but not on Lambda. I have tried various solutions from Stack Overflow, but none of them seem to be working for me. One suggestion was to make the sendEmail fun ...

Events are not being received by the Discord.js client

My client is not receiving any interactions (slash commands) even though it should be able to handle them require("dotenv").config(); const { Client } = require("discord.js"); //disc = require("discord.js"); const axios = re ...

Utilizing a controller's promise feature to invoke a service function in AngularJS

Incorporated within my base controller is a service called Uom. The service is invoked within the controller in the following manner: Uom.get_measurement_unit_conversions().then( function( measurement_unit_conversions ) { // Executing some operations ...

The use of DIV tags allows the element to be displayed in an inline

In my project, I decided to add three div tags. The first two are positioned next to each other with the third div tag placed below them. However, when I tried to insert a slideshow into the first div tag, all of the div tags ended up displaying as "block" ...

Adding options to a dropdown menu dynamically while editing a form with the help of javascript

I have a dropdown like in below:- <form name="depositForm" action="<?php echo site_url('ajax_funds/deposit_funds'); ?>" id="depositForm" class="page-form-main form-horizontal " autocomplete="off" method="post"> <div class="form- ...

Leveraging redux within your NEXT.JS project

While working on my Next.js application, I have integrated Redux and Redux Saga. I am trying to incorporate server-side rendering for making HTTP requests: export const getStaticProps = wrapper.getStaticProps(async ({ store }) => { store.dispatch(g ...

Utilizing Javascript to load and parse data retrieved from an HTTP request

Within my application, a server with a rest interface is utilized to manage all database entries. Upon user login, the objective is to load and map all user data from database models to usable models. A key distinction between the two is that database mode ...

Python script using selenium webdriver to interact with an accordion container and expand its contents (round

After successfully creating a scraper, I encountered an issue where the data I needed to scrape was hidden and required manual expansion before scraping. Upon inspecting the webpage source code, I found that the data was located within 3 different accordio ...

Angular 2's NgFor directive is designed to work with data structures that can be iterated over,

Currently, I am implementing the Reactive form pattern in my Angular 2 project. The goal is to dynamically populate a container with answers that are fetched and populated using an http call. The structure of the Reactive form object is as follows: publi ...

Ways to Conceal <div> Tag

I need help with a prank .html page I'm creating for a friend. The idea is that when the user clicks a button, a surprise phrase pops up. I have managed to hide and unhide the phrase successfully using JavaScript. However, my issue is that when the pa ...

The error message [ERR_HTTP_HEADERS_SENT] is indicating that headers cannot be modified after they have already been sent to the client in a node.js and express

Attempting to retrieve data by Id from records stored in a MSSQL database. For example, trying to make a GET request in Postman like this: localhost:3200/api/v1/players. Encountering an error message: node:_http_outgoing:576 throw new ERR_HTTP_HEADERS_ ...

Transforming an Angular 11 HTML template into Angular code

After attempting to transfer the Porto Admin HTML template to Angular, I encountered some issues. When including the CSS and JS dependencies in the project, everything worked fine with all the HTML code in index.html. However, when I moved the code to app. ...