Exploring the method of including a mat-chip-list within a form

Can't submit form with mat-chip-list elements, even though they are present. How to send the tag array? Please assist!

View my form here

Here is the code I have so far:

<mat-form-field class="example-chip-list">
          <mat-chip-list #chipList>
            <mat-chip
              *ngFor="let tag of Tags"
              [selectable]="selectable"
              [removable]="removable"
              (removed)="remove(tag)">
              {{tag}}
              <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
            </mat-chip>
            <input
              placeholder="tags"
              [formControl]="tagsSet"
              [matChipInputFor]="chipList"
              [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
              [matChipInputAddOnBlur]="addOnBlur"
              (matChipInputTokenEnd)="add($event)">
      </mat-chip-list>
        </mat-form-field>

     removable = true;
  addOnBlur = false;
  separatorKeysCodes: number[] = [ENTER, COMMA];
  Tags: string[] = [];
this.testSuiteForm = new FormGroup({
     
     tagsSet: new FormControl(null, [Validators.required]), 
    });
add(event: MatChipInputEvent): void {
    const input = event.input;
    const value = event.value;
    // Add our tag
    if ((value || '').trim()) {
      this.Tags.push(value.trim());
    }
// Reset the input value
    if (input) {
      input.value = '';
    }
    this.tagsSet.setValue(null);
  }
  remove(Tags: string): void {
    const index = this.Tags.indexOf(Tags);
    if (index >= 0) {
      this.Tags.splice(index, 1);
    }
  }

  get tagsSet() {
    return <FormArray>this.testSuiteForm.get('tagsSet');
  }

Answer №1

When invoking the add() method in a TypeScript file, you have the option to utilize the setValue function for the formControl.

For adding JSON-formatted strings:

this.testSuiteForm.controls['tagsSet'].setValue(JSON.stringify(this.Tags));

Alternatively,

For adding comma-separated strings:

this.testSuiteForm.controls['tagsSet'].setValue(this.Tags.toString());

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

The error message "Property 'zip' is not available on the 'Observable' type in Angular 6" indicates that the zip property is not recognized by

I've been working with Angular 6 and I've also looked into using pipes, but I couldn't find the correct syntax for writing a zip function and importing it properly. Error: Property 'zip' does not exist on type 'typeof Observ ...

Utilizing PrimeNG dropdowns within various p-tree nodes: Distinguishing between selected choices

I am working with a PrimeNg p-tree component <p-tree [value]="treeNodes" selectionMode="single" [(selection)]="selectedNode"></p-tree> The treeNodes are defined using ng-templates, with one template looking li ...

I'm having trouble establishing a connection with the Appwrite platform

Encountered an issue while trying to connect to appwrite. The specific error message is: Uncaught (in promise) TypeError: Failed to construct 'URL': Invalid URL at Account.<anonymous> (appwrite.js?v=d683b3eb:932:19) at Generator.nex ...

Material UI TreeView: Organize and present node data with multiple columns in a tree structure

const treeItems = [ { id: 1, name: 'English', country: 'US', children: [ { id: 4, name: 'Spring', country: 'Uk', ...

Is there a way to omit type arguments in TypeScript when they are not needed?

Here is a function I am currently working with: function progress<T>(data: JsonApiQueryData<T>): number { const { links, meta } = data.getMeta(); if (!links.next) { return 1; } const url = new URL(links.next); return parseInt(url ...

Creating and setting a selected option dynamically in Angular 2 for editing purposes

As I attempt to modify a user, I encounter a scenario where the user possesses a non-primitive array of machines. During editing, my goal is to generate new elements with select options and assign the selected value based on the user object: export class ...

What is the best way to attach a value to an attribute of a web component in Angular?

In all honesty, I am quite new to Angular and still learning the ropes. Currently, I am faced with a debugging challenge related to one of the users encountering an issue with my web component library. Within this library, there is a custom element create ...

What is the best way to limit the types of function parameters in TypeScript based on whether the parameter index is even or odd?

My goal is to create a function with an unlimited number of parameters, where the type of each parameter is determined by whether its index is odd or even. For example: flow(isMachineReady(), 'and', isWaterHot(), 'or', isMilkHot(), &ap ...

Have there been any instances of combining AngularJS, ASP.NET-WebApi, OData, Breeze.js, and Typescript?

I am attempting to combine different technologies, but I am facing challenges as the entity framework meta-datas are not being consumed properly by breeze.js. Despite setting up all configurations, it's proving to be a bit tricky since there are no ex ...

Issues with the functionality of Angular Firebase Authentication Service

I am currently working on setting up an authentication service in Angular that will integrate with Google Firebase for a Login form. However, I have encountered an issue where including the service in the constructor of my LoginComponent prevents me from a ...

Transform Sass modules into css during the creation of a component library

I'm in the process of developing a React TypeScript component library that will be utilized in various projects. Currently, I have been using the following script to build this project. "build": "rimraf dist && NODE_ENV=product ...

Encountering a 405 error when making an OpenAI API call with next.js, typescript, and tailwind CSS

I encountered a 405 error indicating that the method request is not allowed. I am attempting to trigger an API route call upon clicking a button, which then connects to the OpenAI API. Unsure of my mistake here, any guidance would be highly appreciated. E ...

What is the process for deploying a .NET Core + Angular single page application to multiple environments?

After utilizing the Angular project template with ASP.NET Core in Visual Studio 2019, I developed an application that includes multiple environments: DEV, STG, and Production. Each environment corresponds to specific "appsettings.json" files for the .NET p ...

Guide on embedding child components in content using ngx-markdown within Angular version 14

I've been struggling to find a solution to this issue for some time now. We are receiving HTML content from an epi-server that needs to be dynamically rendered in a component. While ngx-markdown works well with standard HTML elements, it seems to ign ...

Creating a cutting-edge Angular 2 project with the power of webpack

Previously, I created a sample Angular 2 application using System JS for module loading, which was functioning properly. Now, I decided to switch to webpack module bundler instead of system JS and made the necessary changes. However, upon running the app ...

A solution for resolving the RuntimeException in genuitec's TypeScript Editor for Eclipse Oxygen

After setting up a fresh Eclipse Oxygen and installing the Angular IDE from Genuitec, I encountered an issue on the second day when I opened a project and accessed a *.ts File. The error message displayed was: java.lang.RuntimeException: java.lang.Illegal ...

Combining and linking 3 RxJS Observables in TypeScript and Angular 4 without nesting to achieve dependencies in the result

I have 3 observables that need to be chained together, with the result from the first one used in the other 2. They must run sequentially and each one should wait for the previous one to complete before starting. Is there a way to chain them without nestin ...

What is the best way to display just the selection outcome?

Currently, my code displays a full list of clinics. When I select a province in the dropdown menu, it only shows the clinics located in that specific province. I would like to modify this behavior so that the full list of clinics is not visible initially ...

Protecting scripts using bypassSecurityTrustStyle in Angular

Is there a way to remove all script tags from a string while preserving the style? Consider this code snippet where we sanitize the style of a string: getSanitized(s: string) { const safeStyle: any = this.sanitizer.bypassSecurityTrustStyle(s); re ...

Having trouble customizing the toolbar on ngx-quill? Quill seems to be having trouble importing modules

UPDATE: I jumped ship when I discovered that PrimeNg had a quill implementation, and since I was already using PrimeNg, I switched over. Initially had some issues, but upgrading to angular 7 and ngrx 7 beta resolved them. https://www.primefaces.org/primeng ...