Tips for effortlessly incorporating a new chip while maintaining a neat arrangement and ensuring button alignment

I am looking to enhance my website by adding chips with new tags in a neatly organized manner. Specifically, I want each new chip to be positioned next to the previous one and have the add-button shift to the right side, always staying in front of the last chip created. While I have already implemented the functionality for adding a new chip, I am seeking guidance on how to achieve this specific ordering and alignment. Unfortunately, I have been unable to find any tutorials or videos online that address this issue. Any advice or code examples you can provide would be greatly appreciated. My current code is displayed below.

HTML

<ion-chip #chip *ngFor="let tag of tagName">
  <ion-label>{{tag.tag}}</ion-label>
  <button ion-button clear color="dark" (click)="remove(chip)">
    <ion-icon name="close-circle"></ion-icon>
  </button>
</ion-chip>

<button class="buttonIcon" ion-button small round icon-only (click)="add(chip)">  
  <ion-icon name="add"></ion-icon>
</button>

TS

public tagName = [
    {
      "tag": "#men"
    },
];

 add(chip: Element) {
    this.tagName.push();
  }

Answer №1

If you want to keep things uncomplicated, just remember to use the push method for adding elements and the splice method for removing them. When adding a new element, no parameters are required, but when removing one, simply provide the ID of the element.

export class Tag {
  tag: string;
  constructor(values: Object = {}) {
    Object.assign(this, values);
  }
}

...
  tagName: Tag[] = [];

  add(): void {
    let id = this.tagName.length + 1;
    this.tagName.push(new Tag({ tag: "tag" + id }, ));
  }

 remove(id: number): void {
    this.tagName.splice(id, 1);
 }

HTML

   <ion-chip #chip *ngFor="let tag of tagName; let i = index">
        <ion-label>{{tag.tag}}</ion-label>
        <button ion-button clear color="dark" (click)="remove(i)">
           <ion-icon name="close-circle"></ion-icon>
        </button>
  </ion-chip>


  <button class="buttonIcon" ion-button small round icon-only (click)="add(chip)">  
     <ion-icon name="add"></ion-icon>
  </button>

Demo

Answer №2

To have your add button aligned on the right side, wrap it in an ion-chip element.

You can achieve this by following the code snippet below:

HTML Code:

<ion-chip #chip *ngFor="let tag of tagName">

 <ion-label>{{tag.tag}}</ion-label>
  <button ion-button clear color="dark" (click)="remove(chip)">
    <ion-icon name="close-circle"></ion-icon>
  </button>
    </ion-chip>

    <ion-chip color="primary">
    <button ion-button clear color="light" (click)="add(chip)">
      <ion-icon name="add"></ion-icon>
    </button>
    </ion-chip>

For a live demonstration, you can check out the stackblitz demo.

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

Resolving the error message "Default props must have construct or call signatures for 'Component' in Typescript"

I'm currently working on a function component called MyComponent and I'm facing an issue with setting a default prop for component. The goal is to have the root node render as a "span" if no value is provided. However, I am encountering the follo ...

Steps to deactivating a styled button using React's styled-components:

I've created a very basic styled-components button as follows: import styled from 'styled-components'; const StyledButton = styled.button``; export const Button = () => { return <StyledButton>Default label</StyledButton> ...

Routing with nested modules in Angular 2 can be achieved by using the same

Encountering a common issue within a backend application. Various resources can be accessed through the following routes: reports/view/:id campains/view/:id suts/view/:id certifications/view/:id Note that all routes end with the same part: /view/:id. ...

What is the most effective method for achieving a desired outcome?

Is it a valid approach to get an action result, and if so, how can this be achieved? For instance, if there is a page with a form for creating entities, after successfully creating an entity, the user should be redirected to the entity's detail view. ...

Angular 7: Child component's OnChanges not firing under specific circumstances

I incorporated a child component into my parent component. Initially, the Child component's onChanges method is triggered via ngOnInit. However, upon clicking the onClick function, the Child component's onChanges method fails to trigger. What cou ...

Tips on setting the first root element to automatically expand in a tree component

Currently, I am utilizing a tree component that includes partially loaded data. For reference, here is the link to the stackblitz example: StackBlitz. Is there a way for me to have the child element of the first root element automatically opened by defau ...

Unresolved promise: Issue encountered with StaticInjectorError within AppModule linking to HttpHeaders:

I am currently working on an ionic v3 project and facing a particular issue. The problem is similar to #47492475, even though I have already imported HttpClientModule in app.module.ts. Despite this import, the error continues to persist. Below are my .ts f ...

What steps should be taken to resolve the error message "This Expression is not constructable"?

I'm trying to import a JavaScript class into TypeScript, but I keep getting the error message This expression is not constructable.. The TypeScript compiler also indicates that A does not have a constructor signature. Can anyone help me figure out how ...

Using ThreeJS to Apply Dual Materials to a Mesh Entity

With ThreeJS, it's possible to incorporate more than one material into an Object3D/Mesh as stated in the documentation. You can either utilize a single Material or an array of Material: Class declaration and constructor for Mesh TypeScript file (exce ...

Is there a way to transfer ngClass logic from the template to the TypeScript file in Angular?

I am implementing dropdown filters for user selection in my Angular application. The logic for adding classes with ngClass is present in the template: <div [ngClass]="i > 2 && 'array-design'"> How can I transfer this ...

The variable 'xxx' is not defined in this context

I am attempting to incorporate a dark theme into ngx-charts, and I'm relatively new to using less. Here is the code snippet: My IDE is showing an error message "Cannot find variable 'color-bg-darker'" which leads to compilation failure. Err ...

Adding onBlur validation for radio buttons and checkboxes in Angular Material UI

Currently, I am working on implementing checkboxes and radio buttons using Angular Material UI. My main issue lies in achieving the desired red outline effect when a required field is left unselected by the user. Even after applying the necessary 'req ...

Learn how to dynamically enable or disable the add and remove buttons in the PrimeNG PickList using Angular 2

I'm currently learning Angular 2 and I'm working on creating a dual list box using PrimeNG's pickList component (https://www.primefaces.org/primeng/#/picklist). Within the pickList, I have table data with 3 columns, along with ADD and REMO ...

Transform ECMAScript Observable / Zen Observable into RXJS Observable

I currently have an ECMAScript Observable that is compliant with specifications, specifically sourced from the wonka library. I am facing challenges in converting this type of observable to an rxjs 6 observable. It appears that this conversion was possibl ...

After the assignment, TypeScript reordered the elements of the array

Dealing with an array of objects for use in a ngFor loop has presented a challenge. The issue arises when the order that was initially set for the array changes unexpectedly due to JavaScript manipulation. Originally, the array is ordered as expected when ...

Padding for the doughnut chart in Chart.js canvas

My current setup includes a canvas featuring a doughnut chart created with chart.js enclosed in a div element that displays like this: The canvas has a red background to enhance visibility. Currently, the chart is centered within the canvas with additiona ...

Selecting ion-tabs causes the margin-top of scroll-content to be destroyed

Check out the Stackblitz Demo I'm encountering a major issue with the Navigation of Tabs. On my main page (without Tabs), there are simple buttons that pass different navparams to pre-select a specific tab. If you take a look at the demo and click t ...

You appear to be missing a dependency on either "@angular/core" or "rxjs" when attempting to deploy your MEAN app on Heroku. This issue must be resolved

I encountered an issue while trying to deploy my MEAN-stack application on Heroku. My app was built mainly following this tutorial series. However, during the deployment process by pushing to GIT, I received the following error: <a href="/cdn-cgi/l/emai ...

Creating new Angular2 Observables - Subscribing to undefined developments

Is it a scoping issue or a problem with how my observables are set up? Can you assist me in figuring it out? To explain my logic: My goal is to first check if the data is available locally before making an http.get request for it. I want to return the loc ...

Exploring Variables in Angular 11 HTML: A Guide to Debugging

The challenge In my Angular 11 web app, I am trying to troubleshoot and inspect the variables within the HTML code. While I have managed to retrieve the ctx.ngForOf variable, I feel like there is something missing in my process that I can't quite pi ...