Resetting a Template-Driven form in Angular using programming techniques

Exploring Angular/Typescript as a newcomer with the use of Template-Driven forms in Angular 10. Attempting to reset the form without submitting it or relying on button clicks. Despite researching similar issues, they all entail submitting the form for a reset, resulting in the following error:

ERROR TypeError: Cannot read property 'reset' of undefined

The TS file code looks like this:

@ViewChild('visitorForm', { static: true }) visitorForm: NgForm;

constructor(private myService: MyService)
{
  this.myService.imagePosted.subscribe(
    userDetails => {
      this.userDetails = userDetails;
     this.visitorForm.reset();
    }
  );
}

Here is the component.html code snippet:

<form (ngSubmit)="onSubmit(visitorForm)" #visitorForm="ngForm">
  <div class="row">
    <div class="col-md-10 form-group">
        <input 
        [ngModel]="visitor.firstName"
        required 
        type="text" 
        id="firstName" 
        name="firstName" 
        placeholder="First Name" 
        class="form-control"
        #firstName="ngModel">
    </div>
  </div>
</form>

I have successfully managed to clear the form upon submit or using a button with an event binding, but that's not the desired outcome. Seeking a solution to reset the form within the subscribe function above. Any insights on how to achieve this?

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

Tips for displaying a multi-select dropdown in the Creative Tim Angular Material Pro filter panel

I am in need of assistance with modifying the standard Creative Tim Angular Pro Material template due to my limited CSS/SCSS skills. Could someone provide examples of the necessary changes, whether it involves altering the HTML or multiple CSS files withi ...

Connecting table checkboxes to buttons in Angular 4.x: A step-by-step guide

Exploring Angular 4.x for the first time and faced with a challenge. I have created an HTML table where each row contains a checkbox and buttons. My goal is to link the checkboxes with the buttons in such a way that when a checkbox is checked, the correspo ...

unable to retrieve the value from the scope

I'm trying to implement the following code in HTML: <div ng-if="item.shareText"> <textarea></textarea> <div> <select ng-model="shareOptions"> <option value="PUBLIC" selected>public</option> ...

Utilizing Jest to Simulate a Class - Incorporation via Imported Module

I'm having difficulty mocking a constructor of a class from an imported module. Interestingly, it works perfectly when my mock implementation is directly inserted into the jest.mock() factory function, but fails when the implementation is imported fro ...

Can the data cells of columns be dynamically adjusted to align them on a single vertical line?

For some time now, I have been grappling with a CSS issue. I am working with a table that has 3 columns displaying departures, times, and situational text for scenarios like delays or cancellations. However, as evident from the images, the alignment of th ...

Binding in Angular for internationalization (i18n)

What is the best way to translate a binding using Angular's built-in i18n feature? //translating attribute works fine <mycomponent i18n-myattribute myattribute="just an attribute"></mycomponent> //how to handle translating bi ...

The 'ngForOf' property cannot be bound to 'tr' as it is not recognized. Make sure to import CommonModule and BrowserModule

Encountering a strange issue. When attempting to use *ngFor on a tr element, an error is thrown: <table> <tbody> <tr *ngFor="let item of productsData"> <!-- some Code --> <tr> </tbody> </table> Within my ...

What are some ways to make autorun compatible with runInAction in mobx?

Currently delving into the world of mobx and runInAction, facing a challenge in comprehending why autorun fails to trigger my callback in this particular scenario: class ExampleClass { // constructor() { // this.exampleMethod(); // } ...

Looking for a regular expression to require either a dollar sign ($) or a percentage symbol (%) but not

At the moment, I currently have this input field set up on an HTML page within my Angular 9 application: <input type="text" formControlName="amountToWithholdInput" onkeyup="this.value = this.value.replace(/[^0-9.%$]/, &ap ...

Issue with MSAL v2 Angular in Microsoft Teams Desktop Application

We are currently experiencing an AAD login issue with our web application (built using node.js + angular 10) specifically in MS Teams Desktop & Mobile app. Interestingly, users are able to easily login by simply clicking a login button in any web browser o ...

The Mui datepicker displays the day and month in reverse order

Do you have trouble with date search filter using the mui datepicker component? It seems that when entering a date in the input and clicking on search, the day gets swapped with the month. For instance, if you input 03/05/2022, it returns Sat Mar 05 2022 ...

Updating headers with Angular HttpClient during a lazy update#Unique Version:

In my Angular application, I have been utilizing the HttpClient module from @angular/common/http for making API requests. Everything was running smoothly with POST, GET, and DELETE requests to the backend URL with the necessary Bearer Authentication header ...

Tips for implementing validation in template-driven form using Ionic 3

How can I validate a mobile number in an Ionic 3 application? I am using ngModel, but I'm looking for an example to help me with the validation process. Can anyone provide guidance on how to do this? <ion-list> <ion-item margin ...

What's the best way to use Flexbox to center a component with a "fixed width"?

As I work on my component, here is the code snippet from my highest parent, App.component: app.component.html <div class="wrapper_flex"> <div style="width:400px; background-color: pink;"></div> <div style=& ...

Breaking down the steps to flip between two images when clicked in Vue.js

I'm currently trying to implement a feature in Vue.js where images switch on click. The functionality I'm aiming for is as follows: Upon initial load, the image displays in black, then upon clicking/selecting it, the image transitions into a blu ...

Having trouble with react-i18next not working properly in my React Native application

I recently initiated a new react-native project, but I seem to be encountering an issue with my react-i18next translations. Despite having the keys correctly set up, I am unable to view the translations. Furthermore, I have noticed that my components are ...

Error: Unable to call function onPopState from _platformLocation due to TypeError

After building an angular application, I encountered a strange issue where it runs smoothly in non-production mode but throws an error when running with --prod: Uncaught TypeError: this._platformLocation.onPopState is not a function I have double-checked ...

Blending of Typescript Tuples

Is there a way to merge tuples in TypeScript such that one tuple is added at the end of another? Here is an example: type MergeTuple<A extends any[], B extends any[]> = [...A, ...B]; I have tried the following approach: type MergeTuple<A extend ...

What is the process for updating nodemailer to integrate with https (SSL) connections?

I'm in the process of setting up an Angular website, and I've encountered a roadblock with the contact form. Initially, the form was functional on the insecure version of the site. However, after implementing a URL rewrite rule to force users to ...

ng2 - Comparing DevExtreme and Telerik Kendo UI

Our team is embarking on a new software project utilizing Angular2, typescript, and HTML5. We are considering two options for UI components: - DevExtreme - Telerik Kendo UI Which of these would be the best choice in your opinion? Thank you! ...