How can we prevent new chips (primeng) from being added, but still allow them to be deleted in Angular 2?

Hey there! I'm currently exploring how to make the chips input non-editable. I am fetching data objects from one component and using the keys of those objects as labels for the chips.

Check out my HTML code below:

<div class="inputDiv" *ngFor="let k of key">
  <div class="inlineclass">
      <span class="title">{{k}}</span>
          <p-chips [(ngModel)]="filterInput[k]" ></p-chips>
  </div>
</div>

And here's the component where I'm retrieving my data:

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'itc-upc-itc-component-filter',
templateUrl: './itc-component-filter.component.html',
styleUrls: ['./itc-component-filter.component.scss']
})
export class ItcComponentFilterComponent implements OnInit {
filterInput= { 'size': ['1T', '2T', '3T'], 'status': [ 'in progress','complete', 'pending']};
key= Object.keys(this.filterInput);
constructor() { }

ngOnInit() {
}

}

I've attempted using the disabled property on ng-prime's chips component:

<p-chips [(ngModel)]="filterInput[k]" disabled="true"></p-chips>

Is there a way to make this input uneditable while still allowing users to delete the chips?

Answer №1

To ensure that only the backspace and delete keys are allowed for keyboard events, listen specifically for those key presses. This will allow you to remove characters without adding any.

HTML:

  <span *ngIf="filterInput[k]" class="title">{{k}}</span>
  <p-chips (keydown)="onChange($event)" [(ngModel)]="filterInput[k]" ></p-chips>

Typescript

   onChange(event){
       if (!(event.keyCode == 8 || event.keyCode == 46)){
           return false
       }

   }

DEMO

Answer №2

Working with Angular and Lodash

 <p-inputText [(ngModel)]="inputText" (keypress)="onKeyPress($event)"></p-inputText>
import {TAB, ESCAPE} from '@angular/cdk/keycodes';
import * as _ from 'lodash';

onKeyPress(event) {
  return _.has([TAB, ESCAPE], event.code);
}

Answer №3

<p-chips [(ngModel)]="filterInput[k]" [inputStyle]="{ visibility: 'hidden' }"></p-chips>

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

Issues arise after upgrading Node and npm, causing an inability to execute any npm command

Following an upgrade to the latest Node and npm version, I encounter an error when attempting any npm command: C:\Users\...>npm doctor TypeError: Cannot read property 'prefix' of undefined at parseField (C:\Users&bs ...

Angular 6 issue: There was an error attempting to differentiate '[object Object]'. Only arrays and iterables can be used

Could you please assist me in resolving this issue? ProfileComponent.html:4 ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed at DefaultIterableDiffer.push../node_modules/@angular/core/fesm5/core.js.D ...

Trustpilot Authentication Error: Grant_type Not Recognized

I am attempting to utilize the Trustpilot API for sending email review invitations. Before making the call, I need to obtain an access token as per Trustpilot's documentation. However, when implementing the function below, I am encountering an error i ...

Is there a way to verify if multiple variables in Typescript are null or undefined?

Background To address the issue of checking whether a specific variable is null or undefined, I created the isNullish function. The implementation of this function is shown below. type Nullish = null | undefined; const isNullish = (target: unknown): targe ...

Angular has its own unique way of handling regular expressions through its TypeScript

Considering the creation of an enum to store various regex patterns in my application for enhanced code reuse. For example: export enum Regex { ONE_PATTERN = /^[example]+$/g, ANOTHER_PATTERN = /^[test]{5,7}$/g } However: Encountering the TS90 ...

What's the best way to use Flexbox to center a component with a "fixed width"?

As I work on my component, here is the code snippet from my highest parent, App.component: app.component.html <div class="wrapper_flex"> <div style="width:400px; background-color: pink;"></div> <div style=& ...

Step-by-step guide on implementing a draggable component for selecting the year using React

I am looking to develop a draggable component in React without relying on any third-party library. Below, I have provided an image depicting how the component might look. Currently, my component code appears as follows: import React from 'react'; ...

Is there a way for me to input an event in handleSumbit?

I am having trouble understanding how to implement typing in handleSubmit. Can someone help? It seems that the "password" property and the "email" property are not recognized in the "EventTarget" type. import { FormEvent, useState } from "react" import { ...

Adding a baseURI to the image src in Angular 5 is causing issues with dynamically loading images

I am currently working on Angular 5.2.1 and I am facing an issue with loading an image from a server using its source URL. Here is the HTML code: <img #image [src]="cover" class="img-fluid" alt="NO image"> And here is the TypeScript code in image- ...

Manipulate the elements within an array, make changes, and then insert

In the array called newData, I am trying to add one more element with Rank 1. However, the issue is that the Rank value is getting updated for both records. The desired behavior is to have Rank set to 1 for the second record and have the first record' ...

Angular - Verify the presence of queryParams on the root path (/)

I have a legacy application built in Vaadin that requires migration to Angular 15 while maintaining the same URL structure. The transition is nearly complete, with one crucial task remaining: identifying if there are no URL query parameters present on the ...

Sending data to the makeStyle function in TypeScript

How can I set the style of backgroundImage in my React component based on the value of post.mainImage? Here is the code snippet: import React from 'react'; import { Post } from '../interfaces'; import { makeStyles, createStyles, Theme ...

Can you please provide guidance on setting the return type to React.StatelessComponent?

This code is functioning correctly: import * as React from 'react'; export default (props: any): JSX.Element => { return ( <h1>{props.children}</h1> ) } However, this snippet is causing an error: import * as React from ...

How can I include components in the precompile array in Angular2 RC4?

After upgrading my Angular2 project to RC4, the router started displaying a warning message in the console when I launch my application: router.umd.js:2466 'FrontpageComponent' not found in precompile array. To ensure all components referred to ...

Error encountered in Angular10: XHR request for POST failed due to browser blocking HTTP Request to a domain with a similar name

I am encountering an issue with my webpage that consists of a Django REST API and an Angular 10 Frontend SPA. Normally, I can successfully send GET, POST, PUT, and DELETE XHR Requests to my backend without any problems. However, there is a specific API End ...

Angular: Implementing asynchronous data retrieval for templates

I am currently facing the following issue: I need to create a table with entries (Obj). Some of these entries have a file attribute. When an entry has a file attribute (entry.file), I need to make a backend call to retrieve the URL of that file: public g ...

Applying REGEX on input text in React Native

I'm having trouble getting my regex function to work correctly, so I believe there might be an error in my code. Any assistance would be greatly appreciated. Here is the regex function I am using: let validatePlate = (plate) => { var re = /(^[A ...

Incorporate the angular-cdk table functionality seamlessly with the sleek design of Bootstrap

Looking to enhance the design of my Angular application by incorporating Bootstrap table styling with the use of Angular CDK table (without Angular Material2). One challenge I've encountered is that while it's possible to add classes to the CDK ...

Failing to retrieve the file instance upon completing the upload process to cloudinary using nestjs

I am attempting to retrieve the secure file URL provided by Cloudinary after successfully uploading the asset to their servers. Although I can upload the file to Cloudinary, when I try to view the response using console.log(res), I unfortunately receive &a ...

Tips for organizing Jasmine unit tests to prevent them from becoming overly lengthy and cluttered

Ensuring that my unit tests are both readable and maintainable is a top priority for me. Instead of cramming all tests into one file, which seems to be the current "best practice," I believe this can actually hinder the quality of the codebase in the long ...