The ion-chip close function activates upon closure, while the ion-item button event is

While trying to utilize an ion-item as a button, I encountered an issue where an ion-chip with a delete event was triggering the ion-item event instead of the intended ion-chip event. Even when using event.stopPropogation, the problem persisted.

Is there a way to properly trigger the onclick event for the ion-chip?

activity.html

<ion-content>
  <ion-list>
    <button ion-item style="color: #999" (click)="addProject()">
      <span *ngIf="selected_project == null">Project</span>
      <div *ngIf="selected_project != null">
        <ion-chip color="primary">
          <span style="margin-left: 10px"><i class="fa fa-book">&nbsp;{{ selected_project.name }}</i></span>
          <button ion-button clear color="light" (click)="deleteProject($event)">
            <ion-icon name="close-circle"></ion-icon>
          </button>
        </ion-chip>
      </div>
      <ion-icon name="add" item-right></ion-icon>
    </button>
  </ion-list>
<ion-content>

activity.ts

addProject(){
   //some code
}

deleteProject(event){
  event.stopPropagation(); //not working
}

Answer №1

The issue at hand is not related to the propagation of the event, but rather to the type of element being used as a button. Ionic performs numerous operations behind the scenes to handle events triggered by buttons. To address this, you can switch the ion-item to simply be an item without the button attribute. You can refer to this functional plunker for more information.

By replacing

<button ion-item ...></button>
with
<ion-item tappable ...></ion-item>
, the visual outcome remains the same, but now the event.preventDefault() function operates correctly.

View

  <ion-list>
     <ion-item tappable style="color: #999; cursor:pointer;" (click)="addProject($event)">
      <span *ngIf="selected_project == null">Project</span>
      <div *ngIf="selected_project != null">
        <ion-chip color="primary">
          <span style="margin-left: 10px"><i class="fa fa-book">&nbsp;{{ selected_project.name }}</i></span>
          <button ion-button clear color="light" (click)="deleteProject($event)">
            <ion-icon name="close-circle"></ion-icon>
          </button>
        </ion-chip>
      </div>
      <ion-icon name="add" item-right></ion-icon>
    </ion-item>
  </ion-list>

Component

@Component({...})
export class HomePage {

  public  selected_project = { name: 'DemoProject'}

    constructor() {}

    public addProject(event) {
    event.stopPropagation();
    alert('Add project');
  }

  public deleteProject(event) {
    event.stopPropagation();
    alert('Delete project ');
  }

}

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

How can we send a parameter to a subscribe function for retrieving the contents of an assets file in TypeScript and Ionic 3?

When working in TypeScript, I have discovered a way to retrieve the content of a text file from my assets directory using the following code: this.http.get('assets/data/file_1.txt') .subscribe(data=>{ let obj = data['_body']; ...

Can you point me to the location where the 'req' parameter is specified?

I've been exploring a new authentication approach detailed in this article. One issue I'm encountering is locating where the req parameter is declared in the snippet below. It seems like my code won't compile because this parameter isn&apos ...

Modifying the variable value in one component by retrieving it from another component

In my Angular app, I have a FAQ component located in the app.component.html file, positioned next to the Router Outlet as shown below: <div class="site-container"> <router-outlet></router-outlet> <app-callback></app ...

Ways to retrieve multiple property values from an array of objects

My data consists of an array of objects which look like this: customer1 = [ {"key": "name", "value": "Peter"}, {"key": "age", "value": 23}, {"key": " ...

Div containing scrollable content with overlaid child element

I am facing an issue with a scrollable div that contains a table, where I am trying to append a loader component as a child to cover the div when it's scrolled. However, the loader only covers the initial size of the div. Here is an example similar t ...

Could the autofill feature in Chrome be turned off specifically for Angular form fields?

Even after attempting to prevent autofill with autocomplete=false and autocomplete=off, the Chrome autofill data persists. Is there a method to disable autofill in Angular forms specifically? Would greatly appreciate any recommendations. Thank you. ...

Typescript indicates that an object may be potentially null

I've hit a roadblock where I keep getting warnings that the objects might be null. After searching online and on StackOverflow, I've tried numerous solutions with no luck. My goal is to insert the text "test" into the HTML elements using their ID ...

Confirm button title by verifying part of the label that contains a space

I'm facing an issue with clicking a button using the following code: await page.getByRole('button', { name: '3 Employees' }).click(); The problem is that the button's name fluctuates based on the number of employees, causing ...

Unable to locate the TypeScript library module within the specified directory

I have been developing a TypeScript NPM package by following instructions from this tutorial You can check out the code here After publishing the NPM package, it is available here Installation of the package is simple using npm install loglevel-file-lo ...

Dynamic Mat-select-trigger that automatically adjusts its size to fit the content

Currently, I am experimenting with an Angular Mat-Select that allows multiple selections. To display the selected values in the value field, I have implemented a custom Mat-Select-Trigger. My goal is to enable automatic resizing of the value field (similar ...

Tips for Incorporating Material Design Lite into Angular2

I have encountered a minor issue while trying to incorporate material design into Angular2. I am using the latest version of Angular2, but the MDL framework seems to be incompatible. Here is what I have done so far: npm install material-design-lite --sav ...

Vue - Troubleshooting why components are not re-rendering after data updates with a method

Check out this simple vue component I created: <template> <div class="incrementor"> <p v-text="counter"></p> <button v-on:click="increment()">Increment</button> </div> </template> <script lan ...

What is the most efficient method for transforming files using a heroku deployed application?

I'm currently developing a Discord bot deployed on Heroku that has a function to convert video files to .mp4 format and then embed the file in a reply message. When running the function locally, everything works fine. However, when running it on the d ...

"Error: The term 'Readable' is not

When I input this code: const { Readable } = require('stream'); Readable ends up being undefined. However, when I try this code instead: import { Readable } from 'stream'; Readable becomes an empty object with no properties. It&apos ...

The call stack size has reached its maximum limit;

Encountering an issue with the use of componentDidMount(). This method is intended to display a Tooltip by utilizing the function _getContentTooltip(). However, the problem arises as it triggers the error message common.js:444 RangeError: Maximum call st ...

Encountering issues with loading series on Highchart in Angular 4 due to duplication restrictions

I recently integrated highchart into my Angular 4 application, but I encountered a problem where the same series is being loaded twice. The issue seems to be related to the addSeries method, which is triggered by the @Input() set stressTestResults declarat ...

Exploring Function Overriding in TypeScript

Currently, I'm working on developing a TypeScript method. import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ p ...

DomSanitizer in Angular is not able to bind to an Angular component

Trying to dynamically insert HTML content into a DIV has been successful when done statically. The following code is functioning properly: <popover-content #pop1 title="Cool Popover" placement="right" ...

What is the best way to extract values based on a key

export class Installment { constructor( public isResurring: boolean, public isInstallment: boolean, public price: string, public sku: string ) { } this.keys = Object.keys(this.paymentPlans); for(let key of this. ...

Verify if a string containing only numerical values contains any non-numeric characters

I have an input field for a phone number, but the type is set to text instead of number. I want to validate it if there are any non-numeric characters present, since I have formatted the input to look like this: 123-123-1234 Here is my input: <input (ke ...