Angular: How to refresh a part of html ( form / div / table )

Tags: , , , ,



I have a page for create operation. In my page I have 1 form with 2 field. If I reload the page (or window.reload by code) I can see updates in that form. But I want to trigger a refresh on the form by pressing button click.

So please help me to write a function that can refresh the form (or any other html element like tabe/paragraph/etc.)

.html of my form:

<form class="was-validated" #zoneForm=ngForm id=#zoneForm>
  <div class=container>
    <div class="row justify-content-center">
      <div class="col-5">
        <div class="card" style="width: 35rem;">
          <div class="card-header">
            <div class="row">
              <div class="col-11">
                Zone Entry
              </div>
              <div class="col-1">
                <i style="margin-left: -1rem;" class="fa fa-arrow-circle-o-right" aria-hidden="true"></i>
              </div>

            </div>

          </div>
          <div class="card-body">

            <div class="row mb-1">
              <div class="col-12">

                <input [(ngModel)]="code" name="code" type="number" class="custom_form_control custom input" placeholder="ID" style="padding: 0px;"
                  disabled>
              </div>
            </div>
            <div class="row mb-1">
              <div class="col-12">

                <input [(ngModel)]="zonename" name="zonename" type="text" class="custom_form_control custom-input" placeholder="Zone Name" required>
                <i class="fa fa-user" style="margin-left: -1rem;font-size: 1rem; color:black;margin-top: 0.25rem;"></i>
              </div>
            </div>

            <div class="row ">
              <div class="col-12 ">
                <button type="button " class="btn btn-outline-primary" style="margin-left: 90%; " (click)="createZone()">Save</button>
              </div>
            </div>

          </div>



          <!-- </div> -->
        </div>
      </div>
    </div>
  </div>


</form>

the function should be called by clicking Save button

Answer

You could use ngOnChanges()

Example:component.ts file

import { Component, Input, OnChanges } from "@angular/core";

@Component({
  selector: "child-component",
  templateUrl: "./child-component.html"
})
export class MyComponent implements OnChanges {
  @Input() someHtml: string;

  constructor() {}

  ngOnChanges() {
  ///** WILL TRIGGER WHEN PARENT COMPONENT UPDATES '**

   console.log(this.someHtml);
  }   
 
}


Source: stackoverflow