Tips for choosing a value using the index in Angular 12's Reactive Forms

Check out the following code snippet:

<select formControlName="test">
   <option value="1">A</option>
   <option value="2"& selected>B</option>
   <option value="1">C</option>
   <option value="1">D</option>
</select>

I have multiple options with the same value. How can I select one by index? Specifically, in my TypeScript code, how can I set the default selected option to be 'C'?

Answer №1

<select name="custom">
   <option *ngFor="let item of customItems; let index = index" [value]="item.value" [selected]="index === 2">{{item.label}}</option>
</select>

in your component ts file:

customItems = [
  {
    value: 1,
    label: 'Apple'
  },
  {
    value: 2,
    label: 'Banana'
  },
  {
    value: 3,
    label: 'Cherry'
  },
  {
    value: 4,
    label: 'Date'
  },
]

link to stackblitz

Answer №2

You have the option to utilize the selected attribute.

<select name="example">
   <option value="1">X</option>
   <option value="2">Y</option>
   <option value="1" selected="selected">Z</option>
   <option value="1">W</option>
</select>

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

Defining a TypeScript interface specifically tailored for an object containing arrow functions

I encountered an issue while trying to define an interface for the structure outlined below: interface JSONRecord { [propName: string]: any; } type ReturnType = (id: string|number, field: string, record: JSONRecord) => string export const formatDicti ...

Unlocking the Power of RxJS with Observable Sharing

Let's take a look at a function that contains the code below: private foo() { let obs: Observable<any> = this._http.get<number>('/foo') obs.subscribe(x=> { console.log("foo : " + x) }); this.blah(ob ...

Automatically upgrade packages to the latest version 12.0.0-next.0 with ng update

Recently, I've encountered an issue while updating my projects from Angular 10 to Angular 11. Whenever I run ng update, some packages are being upgraded to version 12.0.0-next.0 instead of the stable release. It seems like ng update is installing pre- ...

Angular compodoc tool is not considering *.d.ts files

Is there a way to make compodoc include .d.ts files in the documentation generation process for my Angular project? Even though I've added all .d.ts files to tsconfig.compodoc.json as shown below: { "include": [ "src/**/*.d. ...

I'm facing some issues with my initial attempt at an Angular application and it's

After diving into Angular and setting everything up, I encountered an issue where instead of seeing a name displayed, I was getting {{ name }}: https://i.sstatic.net/SXYju.png Below is the content of app.component.html: <input type="text" [(ngModel)] ...

Utilizing Node modules in TypeScript, Angular 2, and SystemJS: A Comprehensive Guide

In the process of developing a simple Angular 2 application, I am constructing a class named User. This class includes a function called validPassword() which utilizes the bcrypt library to validate user passwords: import { compareSync, genSaltSync, hashS ...

Troubles with Angular2 template parser when parsing HTML fragment that is W3C validated

The online W3C validator successfully parses the following document: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>test</title> </head&g ...

I'm encountering a typescript error as I migrate a Paho MQTT function from Angular 1 to Angular 2 - what could be causing this issue?

When connecting to my MQTT broker, I am working on several tasks. In my Ionic 2 and Angular 2 application, I have created an MQTT provider. Here is the provider code: import { Component } from '@angular/core'; import { NavController, ViewControl ...

Need to import Vue component TWICE

My question is simple: why do I need to import the components twice in the code below for it to function properly? In my current restricted environment, I am unable to utilize Webpack, .vue Single File Components, or npm. Despite these limitations, I mana ...

Ionic app: refresher functionality works in browser but not on iOS home screen app

I am currently developing a Progressive Web App (PWA) using Ionic, and I have implemented an ion-refresher in the app: <ion-content> <ion-refresher slot="fixed" (ionRefresh)="refresh()"> <ion-refresher-content pullingIcon="lines">& ...

Learn the art of efficiently saving scanned documents using the Dynamic Web Twain

I'm having trouble saving documents after scanning in my web app that utilises angular 4. I came across this function in the Twain documentation: function DynamicWebTwain_OnPostTransfer() { var strFileName; var Digital = new Date(); v ...

Firebase initialization unsuccessful due to incorrect directory placement

I've been encountering an issue while deploying my Angular 2 project to Firebase. The initial deployment was successful, but subsequent attempts only show the Firebase Hosting welcome page instead of my project in the URL. I've realized that even ...

Developing an Angular project may result in an overflow of memory

After attempting to construct an Angular project on a Linux server with the -aot flag, I encountered an error. Despite upgrading my instance to a higher CPU core, increasing RAM, and enabling a SWAP partition, the error persisted and even occurred more rap ...

Variety of editions tailored to individual clients

In my development of an Angular 6 application that I plan to distribute to multiple clients, there is a need for customization specific to each client while also maintaining common elements. My vision is to organize the directory structure as follows: /s ...

Cypress symbolizes the logical OR condition within a repeating loop

I am currently facing a challenge with testing the input values of a table. I am struggling to represent the OR condition and skipping a specific cell within the table. The table is cyclic in nature, where all values are positive except for one cell that a ...

Ways to determine whether an element is presently engaged in scrolling

Is there a way to determine if certain actions can be disabled while an element is being scrolled? The scrolling may be triggered by using the mouse wheel or mouse pad, or by calling scrollIntoView(). Can we detect if an element is currently being scroll ...

Links do not open in a new tab or new window as intended

I am facing an issue where I can navigate to all links, pages, or components from the base URL, but I cannot open a specific URL like "http://localhost:4200/home/dashboard" in a new tab. Instead, it just shows a blank page. It is worth noting that even wh ...

What is the best way to reduce the viewport size of my application to 67% of the Chrome browser view?

Recently, I acquired an Angular application that comes with hundreds of SCSS files. However, when the application is viewed in a browser set to 100%, it appears ugly and too zoomed in. Applying zoom: 70%; to the index.html body improves the appearance, bu ...

Creating TypeScript Unions dependent on a nested object's property

I want to create a Union Type that is dependent on a nested property within my object. Take a look at the example provided below: type Foo = { abilities: { canManage: boolean } } type Bar = { abilities: { canManage: boolean ...

Creating an Angular 2 component library that is compatible with both webpack.js and system.js: A guide

This is my first venture into creating an Angular 2 library. So far, it consists of a collection of components. I am aiming to make this library compatible with both Webpack and SystemJS. I have successfully written the code for the first component to be c ...