I have an angular app with a login modal wherein I take user input such as his/her phone number. I have binded this phone no to phoneNo
property of the component using [(ngModel)]=phoneNo
. For a case, I need to interact with this app programmatically and submit the login details by entering the phone number in the input field and then clicking on the submit button.
I have tried setting the value of the input by setting value of the element like
document.getElementById('phone').value = 1234567890;
This just updates the value that I can see on screen, but not the property of the angular component. Thus, when I click on the submit button, it takes in the default value residing in the component or the value that I enter using graphical interaction on the website, and not the value that I intended.
I have also tried updating the value using setAttribute("phoneNo", 12345667890)
method, but that also doesn't work. In fact, if I get the names of the attributes using getAttributeNames
method, I don't see any "value" or "phoneNo" attribute, which makes sense because I haven't written these attributes in HTML code.
Using the querySelector method to select the component and then assign the property also doesn't work. The thing is I am not able to get any access to the actual property of this component that I want to change.
I have also tried using formgroups
or formbuilders
in Angular and then setting the value, but then also I don't see any control
and setValue
method.
My angular code:
<form #f="ngForm" id="phone-form" [formGroup]="phoneForm">
<label for="phone">Phone Number *</label>
<div class="d-flex input-box">
<div>+91</div>
<input
type="text"
class="form-control special"
id="phone"
name="phone"
[(ngModel)]="phoneNo"
required
/>
</div>
..............
The input field element that I see in browser:
<input _ngcontent-vkb-c73="" type="text" id="phone" name="phone" required="" class="form-control special ng-untouched ng-pristine ng-valid">