Unique component for customized form controls

Currently, I am delving into the world of Angular. One challenge I have been tackling is creating a custom form control that includes a mat-select. The goal is for the submitted form value to be the item selected in the dropdown.

After sifting through numerous tutorials and documentation, I finally pieced together a solution which you can see in this stripped-down example on StackBlitz.

Alas, despite my efforts, it still doesn't seem to work as intended. Why might that be?

Answer №1

Don't complicate things too much. Implementing ControlValueAccessor is not necessary for a basic scenario like this. Just pass the FormControl to the child component using a regular @Input().

dropdown.component.ts

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

@Component({
  selector: 'dropdown',
  templateUrl: './dropdown.component.html',
  providers: [ ]
})

export class DropdownComponent implements  OnInit {
  @Input() ctrl: FormControl;
  @Input() options: string[];

  ngOnInit() {  }
}

dropdown.component.html

<mat-form-field>
    <mat-select [formControl]="ctrl" placeholder="Select an option">
        <mat-option *ngFor="let option of options" [value]="option">
            {{ option }}
        </mat-option>
    </mat-select>
</mat-form-field>

See live demo here

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

Convert all existing objects to strings

I have a type that consists of properties with different data types type ExampleType = { one: string two: boolean three: 'A' | 'Union' } Is there an easier way to define the same type but with all properties as strings? type Exam ...

Discovering the current page using ons-navigator in Onsen UI

I am currently attempting to determine the page I am on while using the popPage() function with Onsen UI's ons-navigator. I have tried the following methods, but they always return false regardless: this.navigator.nativeElement.popPage().then((page: a ...

Looping through the json resulted in receiving a null value

When working with typescript/javascript, I encountered an issue while trying to fetch the 'statute' from a data object: {_id: "31ad2", x: 21.29, y: -157.81, law: "290-11",....} I attempted to assign data.law to a variable, but received a typeer ...

The Issue with NG-Zorro Datepicker Manual Input Mask Functionality未分类

Is there a way to mask the datepicker so that users can enter dates in the format MM/dd/yyyy without typing the "/" character manually? I've tried using ngx-mask but it doesn't seem to work for the datepicker component. I have provided a link to ...

Typescript struggles to identify properties that have no business being there

In my function for formatting data, the "values" contained within this data are clearly defined. However, TypeScript is failing to recognize new properties that are invalid when mapping these values. The issue can be best understood by looking at the code ...

What is the best way to transfer two distinct states from my ngrx effect to my service function?

I am encountering a dilemma with my effects function, where I am attempting to pass data from a dispatched action and a separate selector to a service function. However, I am finding myself confused by the RXJS syntax and suspect that I may not be mapping ...

Develop a component library for Angular 2 without incorporating inline styles

I recently came across an interesting article about developing an Angular 2 component library that utilizes inline styles. Is there a method to create a library in Angular 2 without relying on inline styles? I also stumbled upon another resource, but it d ...

Warning: NG2 RC5 has deprecated the use of HTTP_PROVIDERS

With the release of Angular2 version RC5, there have been some changes such as deprecating HTTP_PROVIDERS and introducing HttpModule. While my application code is functioning properly with this change, I am facing difficulties in updating my Jasmine tests. ...

Using Angular Material theme with Webpack

In the process of configuring angular material for my Angular (4) application using webpack, I discovered that including a default theme is necessary for it to function properly. One of the recommendations provided in the documentation is to use @import & ...

Implementing debounceTime in Angular can be done by using the debounceTime operator

I am currently working on implementing the debounceTime functionality in an autocomplete feature. My goal is to use debounceTime to minimize the number of server calls. After researching, I found 3 possible solutions which I have included snippets of belo ...

The package import path varies between dynamic code generation and static code generation

I have organized the src directory of my project in the following structure: . ├── config.ts ├── protos │ ├── index.proto │ ├── index.ts │ ├── share │ │ ├── topic.proto │ │ ├── topic_pb. ...

Guide on uploading an image to Azure Blob Storage using v10 SDK

Currently, I am attempting to upload an image to the Blob Storage in Microsoft Azure using SDK V10 within an Angular framework. The code snippet below demonstrates how I establish a connection to the Storage and retrieve the names of all containers: // E ...

Angular progressive web applications (PWAs) continuously fluctuating in their stability,

I have encountered a problem with my Angular application where it never reaches a stable state, as indicated by ApplicationRef.isStable only emitting false once. The Angular docs mention that: the application will never be stable if you start any kind o ...

Why do I keep receiving a <prototype> object for each API request?

Currently, I am utilizing JSONPlaceholder in conjunction with Angular as part of my learning process. While I have been following the documentation meticulously and obtaining the correct output, there seems to be an additional element accompanying each obj ...

Steps to troubleshoot and resolve the @ionic/angular member Event error in Ionic 5

Recently I made the switch from Ionic 4 to Ionic 5, and now I am encountering this error: ERROR in src/app/app.component.ts(4,10): error TS2305: Module '"/node_modules/@ionic/angular/ionic-angular"' has no exported member 'Events'. ...

Error encountered when updating Angular CLI

I am currently attempting to update my Angular project from version 4 to version 6. After numerous failed attempts to upgrade, I decided to uninstall and reinstall the Angular CLI using 'npm uninstall -g angular-cli' followed by a reinstallation. ...

Angular is unable to bind to 'dirUnless' because it is not recognized as a valid property

I am seeking to implement a custom directive that acts as the opposite of *ngIf. Below is the code I have written for this custom directive: import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core'; @Directive({ sele ...

Refreshing Form in Angular 2

When I remove a form control from my form, it causes the form to always be invalid. However, if I delete a character from another input field and then add the same character back in (to trigger a change event), the form becomes valid as expected. Is ther ...

Accessing different pages in Angular 2 based on user type

If I have four pages and two user types, how can we implement access control in Angular 2 so that one user can access all four pages while the other is restricted to only two pages? ...

The functionality of expandable rows on the material table seems to be malfunctioning once sorting is

After implementing expandable rows and sorting in my table based on the samples provided in Angular Material Table, I encountered an issue. When I try to expand a row after sorting the table, the first click appears to have no effect. The second click brie ...