I want to use the property of the input element when form is submitted. I read the concept of Template reference variable which helps in achieving this case in Angular.
When i assign 'id' attribute of input element with a value and later when i tried fetching, it throws an error. It allows only template reference variable to extract the property.
Why not we access the property of the input element by directly invoking its ID attribute
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-test',
// templateUrl: './test.component.html',
template : `
<input type="text" id = "{{idOne}}" #idTwo/>
<button type="text" (click) =
handleClickEvent(id.value)>Submit</button>
<button type="text" (click) = handleClickEvent(idTwo.value)>Submit
Again</button>`,
styles: [`p {color : red}`]
})
export class TestComponent implements OnInit {
handleClickEvent = (value : string) =>{
console.log(value);
}
public idOne = "testID";
constructor() { }
ngOnInit() {
}
}
When i submit 1st button (which invokes ID directly), it throws error saying
cannot read value of undefined.
When i submit 2nd button, it prints correct value that i inputted.
So my question is
Is template reference variable the only way to fetch the property of the HTML element ?
Can you not fetch the property in a traditional way (directly calling id property) ?