Eliminate input from FormControl depending on the value

I am attempting to remove the element from productForm with the value "Ticket stock", but currently I can only remove items based on their status such as invalid, pristine, or dirty.

let desc = "Ticket stock"
// let index = this.productForm.controls.findIndex((control) => control.pristine);
const index = (this.productForm as FormArray).controls.findIndex(
  (control) => control.invalid
);
console.log(index);
this.productForm.removeAt(index);

Answer №1

You're almost there!

  1. Make sure to set the control as type FormGroup.
  2. Retrieve the value of the productDesc control from the FormGroup for comparison.
removeProduct() {
  let description = 'Ticket stock';

  const pos = (this.productForm as FormArray).controls.findIndex(
    (ctrl: FormGroup) => ctrl.controls.productDesc.value == description
    // Or
    // (ctrl: FormGroup) => ctrl.controls['productDesc'].value == description
  );
  console.log(pos);

  if (pos > -1)
    this.productForm.removeAt(pos);
}

Check out the Demo on StackBlitz

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

An error occurs with webpack during postinstall when trying to load TypeScript

I have created a custom package that includes a postinstall webpack script as specified in my package.json file: "scripts": { ... "postinstall": "webpack" } The webpack configuration looks like this: const path = require('path'); ...

Proper utilization of react-hook-form in conjunction with TypeScript and material-ui to display error messages efficiently

Currently, I am using a combination of react-hook-form with typescript and material-ui. My goal is to pass the error message as a helperText to the TextField. I attempted to achieve this by utilizing helperText={errors.email?.message} however, TypeScript ...

Using Angular's mat-tooltip to trigger a function

I am looking to implement a feature where calling the method selections() from a button tooltip triggers a specific action and returns a string to be displayed when hovering over the tooltip. I attempted to interpolate the returned value in the HTML code, ...

Angular2- Handling asynchronous nested API requests

this.obsData .concatMap((vhosts) => this.apiService.findVH(vhosts)) .subscribe((data) => { this.data = data.docs[0] this.data.Locations.forEach((loc, index) => { if (loc.ip === undefined) { ...

Experience the quirk of using Angular's mat-button-toggle-group with the multiple attribute, and uncover the

I'm experiencing a strange issue with the mat-button-toggle. Here is the code snippet: <mat-button-toggle-group formControlName="fcCWShowForms" multiple="true"> <mat-button-toggle value="HE" (change)="this.showHe=!this.showHe"> Button ...

What is the process for coding a prototype-based ES5 class in TypeScript?

Currently, I am in the process of migrating some legacy ES5 projects to TypeScript. These projects contain older-style classes (functions) defined as shown in the simplified examples below: function MyClass(arg1) { this.arg_prop_1 = arg1; this.arg_prop ...

Using an array in Angular's router.navigate method to list queryParams is a common practice

After constructing a queryParams object using a form, I am now navigating: options { ... words: (4) ["chuck", "norris", "vs", "keanu", "reeves"] } Using the navigate method with that object to update the URL's parameters: this.router.naviga ...

In a functional component in Typescript, what data type should be used for a parameter that accepts an array of objects?

const FormData = ({ dataArray }: object[]): JSX.Element => { console.log("Array of Objects",dataArray) //This is a large form component.... }); The dataArray contains multiple objects, how can I specify a specific type for these components ...

Omitting ts/js files from the package with webpack

I am relatively new to webpack and currently working on an angular2 application with webpack. Our requirement includes having a settings file that should not be bundled, allowing the URL in the settings.js file to be changed after bundling. Here is the co ...

Trouble with communication: SignalR not delivering messages in Angular

I am facing an issue while trying to send a message using SignalR and Angular. The problem is that the API endpoint never gets hit on the backend, and there is no status code returned. Even though the hub connection establishes successfully, when I execute ...

pressing the button again will yield a new outcome

I am looking to disable a button (material ui) when it is clicked for the second time by setting disabled={true}. Unfortunately, I have not been able to find any examples related to this specific scenario on StackOverflow. <Button onClick={this.s ...

How to assign a custom validator parameter to a form group in Angular

I'm in a situation where I have a form group and need to validate the end date so it is not earlier than the start date. The challenge here lies in accessing specific fields when the form group is not yet declared. dateFormGroup: this.fb.group({ ...

In ngx-datatable, I’ve set up a unique custom dropdown component within every header cell

I have a custom dropdown component in each header cell of my ngx-datatable. However, when I click on the dropdown, it is going inside the body of ngx-datatable. Can someone please help me resolve this issue? My development environment includes Angular 4.0 ...

What is the reason behind requiring {[param: string]: string | string[]} for the HTTP GET attribute [params]?

Problem Statement An error occurs during type inference in this.http.get() Error TS2322: Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<IInfo[]>'. Type 'ArrayBuffer' is missing the followin ...

Is there a way for me to generate a custom subtype of Error that can be thrown?

I am attempting to create my own custom error in order to handle it differently when catching it. I want to be able to call if(instanceof DatabaseError) and execute my specific handling logic. export class DatabaseError extends Error { constructor(...a ...

What sets apart using `: Interface` versus `as Interface` in Typescript?

I'm struggling to find the distinction between two similar lines of code due to uncertainty about what they are called. Consider the scenario where the following interface is defined: interface Person { name: string; age: number; } What exactly ...

Encountering a problem when trying to launch the Angular development server

In the process of development using the latest version of Angular, encountering a particular issue with the development server startup. An unexpected error occurred: Object prototype may only be an Object or null: undefined See "/tmp/ng-3lbO1f/angular-err ...

The Angular HTTP request is coming back with a status of 0, even though I was anticipating

I need to access a server with various routes. The routes are as follows: app.use('/401', (req, res) => res.status(401).end()); app.use('/403', (req, res) => res.status(403).end()); app.use('/404', (req, res) => res. ...

Radio buttons with multiple levels

Looking to implement a unique two-level radio button feature for a specific option only. Currently, I have written a logic that will display additional radio buttons under the 'Spring' option. However, the issue is that when it's selected, t ...

Exploring routing within a Higher Order Component in React Native

I am looking to implement a file existence check on every page of my app. The idea is that if a specific file exists, the user should be redirected to another page. One solution I have considered is using a Higher Order Component (HOC) for this purpose. A ...