Okay let's see :)
We have different possibilities to get the input text values in angular.
Currently you are using two way data binding, which means that the [] around ngModel is the path from TypeScript value to HTML template. The () goes vice versa, it updates the value in the TypeScript according to the changes you made in the HTML.
So of course we might remove ngModel and get the value of the variable manually into the input with something like:
<input [value]="data.animal" #form>
Still we are missing the other way around, so probably we should add a button or similar and just use a (click) event to update the variable in the TypeScript file after changes in the html.
<button (click)="updateValue(form.value)">Clickme</button>
After creating the function this should work fine.
Of course there is another possibility using ViewChild references.
So first define the reference like previously.
<input type="text" id="form" name="form" #form>
Then in the TypeScript file we declare the reference to the ViewChild.
@ViewChild('form', {static: true}) formElement: ElementRef;
mydata:string="";
constructor(formElement: ElementRef){
this.formElement = formElement;
}
Then mydata will contain the string entered into the input field. The value can also be set again on the input field as default.
You could as well use the native html API with something like document.getElementById and fetch the value of the input, but I will omit this at this point as I would think that the other possibilities are convenient enough.
If you want yo use formControlName there is also a possibility.
<form [formGroup]="nameForm">
<p>
<input type="text" id="name" name="name" formControlName="name">
</p>
</form>
And in the TypeScript file we can access the input like.
constructor( private formBuilder: FormBuilder) {
this.nameForm = this.formBuilder.group({
name: ''
});
}
onClick() {
this.myusername=this.nameForm.get('name')?.value;
console.log('it does nothing', this.nameForm.get('name'));
}
Or you could just use the blur event on the input and send the value using the $event pipeline.
<p><input type="text" (blur)="blurEvent($event)">
Will be accessed like
blurEvent(event: any) {
this._value = event.target.value;
}
Hope this helps.