Best practices for setting up a select dropdown menu?

What is the best way to create a selection list using three different models? Here is my approach:

HTML:

<ng-container [(ngModel)]='user.id' name="id" >
  <select *ngFor="let userRole of userRoles" required>
    <option *ngFor="let role of roles" [ngValue]="role.role_id">
      {{role.name}}
    </option>
  </select>
</ng-container>

However, I am encountering an issue where I get a duplicate of the "select" component. This error message pops up:

ERROR Error: Uncaught (in promise): Error: No value accessor for form control with name: 'id' Error: No value accessor for form control with name: 'id'

Answer №1

Let's begin by examining your model.

export class User {
  constructor(
    public id: number,
    public role: Role,
  ) {}
}

export class Role {
  constructor(
    public id: number,
    public label: string,
  ) {}
}

Next, let's take a look at your select element.

<select [(ngModel)]="user.roles">
  <option *ngFor="let role of roles" [ngValue]="role"> {{ role.label }} </option>
</select>

As a result, you can check out the updated stackblitz 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

Search for specific item within an array of objects

Working on an Angular project, I am attempting to remove an object from an array. To achieve this, I need to filter the array and then update the storage (specifically, capacitor/storage) with the modified array. Here is my function: deleteArticle(id: str ...

When employing class decorators, it is essential to use the Angular2 reflect-metadata shim

Exploring server rendering with Angular2 using the universal starter example inspired me to experiment with gulp instead of webpack. However, I encountered an issue when the server started: /Users/jaumard/IdeaProjects/trails-angular2-isomorphic/node_modul ...

The update operation for the Reference object encountered an error: The first argument includes a function within a

I'm encountering errors while attempting to create a simple cloud function that detects likes on the RD and then adds posts to a user's timeline. How can I resolve this issue? What mistake am I making? (The 2 errors below are from the Firebase ...

Is it possible for a type to include itself within Typescript?

After reading about Typescript, I learned that the 'declare' keyword is similar to the 'extern' keyword in C. It declares a variable that is defined elsewhere, possibly in the browser: declare var Request: { prototype: Request; ...

Examining the reasoning behind comparisons within an Ionic view

When looping through the results in an ion-list, I compare the values dynamically by using Angular's ngIf directive. <ion-list> // Loop the results <ion-item *ngFor="let protocole of protocoles"> <ng-template [ngIf]="{{value}} == ...

Create a feature in Angular that allows for the dynamic addition of input fields that are connected to a generic

I am dealing with a standard key value array cost: {[ key: string ] : number}; I am exploring ways to configure a dynamic input field <input name="costCat" [(ngModel)] = "???"/> <input name="costVal" [(ngModel)] = & ...

Issue with Mat Autocomplete not populating input value when utilized with a formControl

I've encountered an issue with my custom autocomplete component that implements ControlValueAccessor. I'm attempting to set the value from the parent component using form.get('productName').setValue('Product 1');. While this s ...

Trigger the Modal once the navigation step is completed

When navigating to a new page, I need to wait for the navigation process to complete in order for the modal template to be properly displayed on the destination page. public onOpenModal(item) { this.router.navigate([item.link]).then(() => { this. ...

Navigating through the nested object values of an Axios request's response can be achieved in React JS by using the proper

I am attempting to extract the category_name from my project_category object within the Axios response of my project. This is a singular record, so I do not need to map through an array, but rather access the entire object stored in my state. Here is an ex ...

Tips for narrowing the spacing between bars in a bar graph

Is there a way to decrease the space between bars without increasing the width in an example? I only want to reduce the space but keep the width the same. How can this be achieved? https://i.sstatic.net/cRqaQ.png options: { responsive: false, ...

Interface of TypeScript Undetermined

Currently, I am developing a Demo API Wrapper specifically for Roblox. During the development process, I have come across a certain issue that I would like to address. My aim is to send a request and then return all the data in the manner of an API wrapper ...

The Children Element in Next.js and React Context with TypeScript is lacking certain properties

Encountering a mysterious error while trying to implement React's Context API in Next.js with TypeScript. The issue seems to be revolving around wrapping the context provider around the _app.tsx file. Even though I am passing values to the Context Pr ...

Maintain the initial worth even when making alterations

I've been working with Ag-grid and facing an issue. Initially, I load the original data into the grid using this.rowData. I have a function called addRow that successfully adds a row to the top of the existing rows. However, when the reset function ...

When triggering my custom Exception in Angular 2, Angular automatically encapsulates it within viewWrappedDebugError

Developing a substantial Angular 2 application, I've crafted my own Exception to handle errors efficiently. However, it appears that Angular is trying to encapsulate my error within some viewWrappedDebugError. This is the structure of my custom Excep ...

Explore the refined search capabilities within the ng-select dropdown component in Angular

I am currently utilizing the ng-select library for a list of cities, such as: cities = [ {id: 1, name: 'MA, Boston'}, {id: 2, name: 'FL, Miami'}, {id: 3, name: 'NY, New York', disabled: true}, ...

ngRepeat momentarily displays duplicate items in the list

There is a modal that appears when a button is clicked. Inside the modal, there is a list of items that is populated by data from a GET request response stored in the controller. The issue arises when the modal is opened multiple times, causing the list t ...

How to extract a value from a BehaviorSubject within an Angular 6 guard

I have chosen this approach because I have another guard responsible for validating the user based on a token that was previously stored. This is how it was handled in previous versions of rxjs, but now with the latest version you can no longer use map on ...

Contrasting importing a module in app.module versus a component

Angular 5 Can you explain the distinction between importing a module in app.module versus importing it directly into the component where it is needed? In particular, why is it necessary for a module to be included in node modules, app.module, the import ...

Using async await with Angular's http get

In my service component, I have the following code snippet that retrieves data from an API: async getIngredientsByProductSubCategory(productSubCategoryId: number) { const url = '/my-url'; let dataToReturn: any; await this.http.get(ur ...

Angular: Assigning initial value to <select> element

My question relates to a <select> element that is populated from an array as shown below: <select class="form-control form_cbx long-select" formControlName="category" value="Supplier"> <option *ngFor="let cat of userCate ...