I'm learning Angular and have a small test component (Angular 9 - fresh install using CLI).
I have two inputs and am trying to associate each label to its input by putting the id in the 'for' attribute (I know you could also wrap the field in the label). This works for the first input; clicking on the label will set focus to the input. However, clicking on the second label will also set focus to the first input. It doesn't matter if I swap the position of the inputs, the behavior is the same.
Why would this be? Is there a better way to do this in general? I feel like I'm missing something.
restool.component.html
<div>
<label [for]="origin">Origin</label>
<input [formControl]="origin" [attr.id]="origin">
</div>
<div>
<label [for]="destination">Destination</label>
<input [formControl]="destination" [attr.id]="destination">
</div>
restool.component.ts
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-restool',
templateUrl: './restool.component.html',
styleUrls: ['./restool.component.css']
})
export class RestoolComponent implements OnInit {
origin = new FormControl('');
destination = new FormControl('');
constructor() {
}
ngOnInit(): void {
}
}