Button for enabling and disabling functionality, Delete list in Angular 2

I am looking to toggle between the active and inactive classes on a button element. For example, in this demo, there are 5 buttons and when I click on the first button it removes the last one. How can I remove the clicked button? And how do I implement the functionality to toggle between the active and inactive classes?

<ion-item>
    <ion-label >Add</ion-label>
    <ion-input type="text" value="" #newTag (keyup.enter)="addTag(newTag.value)" (blur)="addTag(newTag.value); newTag.value='' "></ion-input>
  </ion-item>
  <button (click)=addTag(newTag.value)>Add</button>
    <button *ngFor="let category of categories" ion-button >{{category}} <span (click)="delete()">X</span></button>

file.ts

@Input()
  newTag: any;
  categories = ['Windstorm', 'Bombasto', 'Magneta', 'Tornado'];
  addTag(newTag: string) {
    if (newTag) {
      this.categories.push(newTag);
    }
  }
  delete() {
    var index = this.categories.indexOf(this.newTag);
    this.categories.splice(index, 1);
  }

Answer №1

Give this code a try. I haven't had much experience with the Ionic framework, but I have worked with Angular2 before.

<ion-item *ngIf="!showButton">
  <ion-label>Add</ion-label>
  <ion-input type="text" value="" #newTag (keyup.enter)="addTag(newTag.value)" (blur)="addTag(newTag.value); newTag.value='' "></ion-input>
</ion-item>
<button (click)="removeButton()">Add</button>
<button *ngFor="let category of categories; let i = index;" ion-button>{{category}} <span (click)="delete(i)">X</span></button>


@Input()
newTag: any;
showButton: boolean = true;
categories = ['Windstorm', 'Bombasto', 'Magneta', 'Tornado'];

removeButton() : void {
  this.showButton = false;
}

addTag(newTag: string) {
  if (newTag) {
    this.categories.push(newTag);
  }
}

delete(index) {
  this.categories.splice(index, 1);
}

Using state for class

Define a boolean variable

stateOfButton: boolean = false;

In your HTML button

<button [class.active="stateOfButton"] (click)="changeState()">Add</button>

You can style it as desired

.active { background: red }

In your Component

changeState(): void {
    this.stateOfButton = !this.stateOfButton;
}

I hope this solution works for you. Best of luck!

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

Encountering a roadblock while trying to install a package using NPM, as the installation process becomes halted at version [email 

Having trouble installing @angular/cli via npm. It seems to get stuck every time while trying to download the package chokidar. https://example.com/image.png Here is some diagnostic information: npm version 5.0.0 node version 8.0.0 OS: Windows 7 ...

Initialization issue detected in Angular's module APP_INITIALIZER

In my auth module, I have implemented a mechanism to import and set up MSAL configuration before initializing the app. I am using APP_INITIALIZER to delay the initialization of the app until the configuration for MSAL-ANGULAR is retrieved. The issue I am f ...

Having problems with route navigation in Angular/Rails when using multiple layouts

Currently, I am in the process of building an AngularJS application that connects to a RoR backend. However, I have encountered an issue when working with multiple layouts. The application is designed to display one layout for unauthenticated users and swi ...

`Unable to update the checked prop in MUI switch component`

The value of RankPermission in the switchPermission function toggles from false to true, but for some reason, the MUI Switch component does not update in the browser. I haven't attempted any solutions yet and am unsure why it's not updating. I&ap ...

Issue with Typescript in react: JSX element lacks construct or call signatures

After upgrading TypeScript, I encountered the error mentioned above in one of my components. In that component's render method, I have the following code: render() { const Tag = props.link ? 'a' : 'div'; return ( < ...

Can multi-page applications be developed using Angular?

As I contemplate developing a food ordering application similar to Zomato, I have observed that Angular-like routing is used on certain pages, specifically during the ordering process after selecting a restaurant. Unlike other sections where server routing ...

Utilize SCSS values within TypeScript by applying them on a class level

let changeClassDisplay = document.getElementsByClassName('sidebar'); for (var i = 0; i < changeClassDisplay.length; i += 1) { changeClassDisplay[i].style.display = 'block'; } I encountered an issue with this code whe ...

Bypass Auth0 HttpInterceptor based on certain conditions

During the transition from Firebase to Auth0, my Angular front-end application authenticates users to either Firebase or Auth0 based on their email address. I am working on configuring the Auth0 AuthHttpInterceptor provided in the Auth0 Angular SDK for SPA ...

What are some reasons to avoid using $controllerProvider.allowGlobals()?

When transitioning from Angular 1.2 to 1.3, experts suggest housing all controllers inside a module to avoid breaking them. While adding just one line to the module's config can also do the trick: angular.module('app').config(['$contro ...

What is the best way to transform a JSON data-storing object into an array within Angular?

I am currently working on developing a machine learning model using tensorflow.js, but I have encountered a roadblock. The issue I am facing is that I have stored my JSON content in a local object, but for it to be usable in a machine learning model, I ne ...

Incorporating D3.js into Angular 6 for interactive click events

Currently working on building a visual representation of a tree/hierarchy data structure using d3.js v4 within an Angular environment. I've taken inspiration from this particular implementation https://bl.ocks.org/d3noob/43a860bc0024792f8803bba8ca0d5e ...

Issue with connecting Angular controller to the view

My Angular controller is defined as follows: mainCtrl.js angular.module("mainCtrl", []) .controller("mainController", function ($rootScope, $location, Auth) { var vm = this; vm.testStr = "If you see this, mainController is active on your page"; ...

Tips for creating an operation within a JSON document?

Today, I am attempting to customize the appearance of my audiobook list. However, when trying to add an aspectRatio key-value pair to each object in my JSON file, I encountered an error. https://i.stack.imgur.com/Qb3TX.png https://i.stack.imgur.com/qTkmx. ...

Angular throwing "provider not found" error when attempting to inject service into controller

I've been diving into a project that incorporates the concept of IIFE, which I'm still in the process of wrapping my head around. My service appears to be functioning well; I've even used some Jasmine to confirm its definition. However, when ...

AngularJS - ng-show not functioning as expected

Using ng-show to toggle the visibility of the navigator. Code in index.html : <div class="container" ng-show="vm.error" nav></div> Code in app.js var siteApp = angular.module('siteApp', ['ngRoute']); siteApp.config(funct ...

What could be causing the ngx-chart TreeMap labels to not display in IE11?

I am currently utilizing ngx-charts version 10.0.1 to create Treemap charts. My Angular version is 7.2.1. The chart functions as expected in Chrome, but the labels do not appear in IE11. Upon page load, I encounter the following errors in the IE console: ...

What is the best way to access data in a node.js application from IONIC typescript via a REST API?

Here is the structure of my service.ts: import { Injectable } from '@angular/core'; import {Http, Headers} from '@angular/http'; import 'rxjs/add/operator/map'; /* Generated class for the PeopleSearch provider. See http ...

various positions for ngb properties

My input field has both a tooltip and a dropdown attached to it using the ngb attributes: <input placement="right" ngbTooltip="Search" [ngbTypeahead]="search" /> The issue I'm facing is that I want the tooltip to appear on the right ...

AngularFire2 Firestore Custom Query: retrieve documents based on current date and time.startTime

Welcome to the world of AngularFire2 and Firestore! My objective is clear: Query data from Firestore where startTime matches currentDateRange. I am facing some challenges with creating a dynamic query in Firestore. After going through the official docume ...

Error in Ionic: Although $state.go successfully changes the URL, the corresponding view fails to load

When attempting to change the state programmatically, the URL updates, but the view does not load. It only works after refreshing the page, however using $window.location.reload(true); feels unconventional. Controller myApp.controller('WelcomeContro ...