How to extract the chosen option from a bound list within an Angular application

Our goal is to avoid using 2-way binding in our component setup:

<select type="text" formControlName="region" (change)="regionChanged($event)">
      <option *ngFor="let region of regionsDDL" [ngValue]="region">{{region.name}}</option>
    </select>

The form is created reactively using the form builder:

this.fb.group({
.
region:...
.
});

In the event handler, we aim to retrieve the entire object linked to the selected option. Is it safe to access the form group value like this?

regionChanged($event) {
  let selectedRegion = this.basicInfoForm.controls["region"].value;
}

Or is there a chance that the form control value may not be updated before the (change) event occurs?

Answer №1

Be sure to assign a ngModel and utilize the ngModelChange() event

<select type="text" formControlName="region" [ngModel]="someValue" (ngModelChange)="regionChanged($event)">
              <option *ngFor="let region of regionsDDL" [ngValue]="region">{{region.name}}</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

What is the best way to troubleshoot the TypeScript error I am encountering in my JavaScript file?

Currently experiencing a TypeScript error within a JavaScript file The issue is within a folder containing only one JavaScript file, and there are no Node.js or package.json files present. I have disabled the TypeScript extensions (not using tslint). Re ...

React with TypeScript: The struggle of getting LocalStorage to work

Currently, I am dealing with persistence in a todo application developed using React and TypeScript. To achieve the desired persistence, I have implemented localStorage. Allow me to share some code snippets: const [todos, setTodos] = useState<todoMod ...

Effectively enhance constructor by incorporating decorators

Is it possible to properly extend a class constructor with decorators while maintaining the original class name and static attributes and methods? Upon reading the handbook, there is a note that cautions about this scenario: https://www.typescriptlang.or ...

Utilizing the variables defined in the create function within the update function of Phaser 3

I'm facing an issue in my game where I can't access a variable that I declared in the create function when trying to use it in the update function. Here is a snippet of what I'm trying to achieve: create() { const map = this.make. ...

Incorporating an external SVG file into an Angular project and enhancing a particular SVG element within the SVG with an Angular Material Tooltip, all from a TypeScript file

Parts of the angular code that are specific |SVG File| <svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="950" height="450" viewBox="0 0 1280.000000 1119.000000" preserveAspectRatio= ...

Angular Authentication Functionality

I need to create a loggedIn method in the AuthService. This method should return a boolean indicating the user's status. It will be used for the CanActivate method. Here is a snippet of code from the AuthService: login(email: string, password: string) ...

Error class not being applied by Knockout validation

I've implemented knockout validation on a text input and the validation is working correctly. However, the errorMessageClass is not being applied to the error message. I must have made a mistake in my setup, but I can't seem to identify it. My H ...

React Scheduler by Bryntum

After successfully discovering some functions related to various actions, I find myself still in need of additional functions: Currently, I am utilizing these functions by passing them directly as props to the Scheduler React Component: - onBeforeEventSa ...

Issue with file upload controller in .net core 2.2 and Angular 7: IFormFile always returns null

Despite thorough research and checking other answers, my controller is not receiving any data. Correct API URIs are used and the request does reach the appropriate controller. Unique Angular Snippet: In the component.html file - input field setup <d ...

Effortless transfer of a module from one TypeScript file to another

I'm facing an issue with importing classes from one .ts file to another .ts file. Here is the structure of my project: I'm attempting to import print.ts into testing.ts This is how my tsconfig.json appears: The contents of my testing.ts are: ...

Angular Materials auto-complete - Presenting choices depending on certain criteria

I have implemented 4 components, each containing a material autocomplete feature with the same set of options. Whenever I select an option in one component, it sets isAvailable to false and disables that option in the other components. The array below show ...

What could be causing the issue: Unable to locate or read the file: ./styles-variables?

I'm currently following a tutorial on how to create responsive layouts with Bootstrap 4 and Angular 6. You can find the tutorial here. I've reached a point where I need to import styles-variables.scss in my styles file, but I keep encountering t ...

Error TS2315: Invalid Type Assignment for Angular 6 ModuleWithProviders

Hey there, I'm encountering an issue that's got me scratching my head. I've shared some of my code in the hopes that it might shed some light on the problem. The problem cropped up as soon as I started working on a Reactive Form. Let me s ...

Issue encountered: Dealing with a deadlock error triggered by a single query following the

I have a question about managing a query that runs across multiple micro-services, which can sometimes lead to deadlocks. const handleExecution = async (id: number): Promise<boolean> => { try { const query = ` UPDATE articles a1 ...

Is it possible to use a combination of pipe, tap, and subscribe in Angular

I am attempting to fetch data using HttpClient in Angular. Here's how my code appears: getData(suffurl: string, id?:number): Observable<any[]> { return this.http.get<any[]>('localhost:5555/DNZ/'+ this.suff_url) .pipe( ...

Decorator in React that automatically sets the display name to match the class name

Is there a way to create a decorator that will automatically set the static property displayName of the decorated class to the name of the class? Example usage would be: @NamedComponent class Component extends React.Component { \* ... *\ } ...

What is the best way to eliminate commas from an array within an Angular project?

I am attempting to retrieve a list of actors from movies; however, in the database I created, each actor's name has a comma at the end of the string. When calling the array, the content shows up with double commas next to each other. I need help figur ...

How can we create external labels for a polar chart in ng2-charts and chart.js, with a set position outside the circular rings?

Currently, I am working on creating a polar chart using Angular along with chart.js version 2.8.0 and ng2-charts version 2.3.0. In my implementation, I have utilized the chartjs-plugin-datalabels to show labels within the polar chart rings. However, this p ...

I have to make sure not to input any letters on my digipas device

There is a slight issue I am facing. Whenever I input a new transfer of 269 euros with the bank account number BE072750044-35066, a confirmation code is required. The code to be entered is 350269. https://i.stack.imgur.com/YVkPc.png The digits 350 corres ...

Nestjs struggles with resolving dependencies

Having trouble finding the issue in my code. (I'm still new to nestjs and trying to learn by working on some apps). The console log is showing the following error: Nest can't resolve dependencies of the UrlsAfipService (?). Please ensure tha ...