admin管理员组文章数量:1289601
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
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
Share Improve this question asked Aug 19, 2020 at 4:52 user12504785user12504785 2- You want ChangeDetectorRef. detectChanges method – JWP Commented Aug 19, 2020 at 4:58
- @JohnPeters can you show me a link with simple example? – user12504785 Commented Aug 19, 2020 at 4:59
5 Answers
Reset to default 2You could use ngOnChanges()
Example:ponent.ts file
import { Component, Input, OnChanges } from "@angular/core";
@Component({
selector: "child-ponent",
templateUrl: "./child-ponent.html"
})
export class MyComponent implements OnChanges {
@Input() someHtml: string;
constructor() {}
ngOnChanges() {
///** WILL TRIGGER WHEN PARENT COMPONENT UPDATES '**
console.log(this.someHtml);
}
}
I remend you to use ReactiveFormModule, because it is so powerful and It is the best way to work with Forms in Angular:
form example:
ponent.ts
public myForm: FormGroup;
constructor(private fb: FormBuilder) {
this.myForm = fb.group({
one: [],
two: []
})
}
submit() {
this.myForm.reset()
}
ponent.html
<form [formGroup]="myForm" (ngSubmit)="submit()">
<input formControlName="one" type="text"/>
<input formControlName="two" type="text"/>
<button type="submit">Submit!</button>
</form>
I remend you to look Angular Reactive Form docs: https://angular.io/guide/reactive-forms
Look this example: https://stackblitz./edit/angular-ivy-zevbtg?file=src/app/app.ponent.html
You should keep in mind that ngModel will be deprecated to work with forms on next versions...
please use ngOnInit() in your method without refresh page.
import { Component, Input, OnInit} from "@angular/core";
@Component({
selector: "my-ponent",
templateUrl: "./my-ponent.html"
})
export class MyComponent implements OnInit {
@Input() someHtml: string;
constructor() {}
ngOnInit(){}
createZone(){
this.ngOnInit();
}
}
public myForm: FormGroup;
constructor(private fb: FormBuilder, private http: HttpClient) {}
ngOnInit(){
this.myForm = fb.group({
one: [],
two: []
})
}
submit() {
this.http.post().subscribe(
(data) => {
this.ngOnInit();
console.log(data);
this.myForm.reset()
})
}
A scenario I had, where I needed to update an HTML structure that used a boolean to show part of the page before calling a method for printing.
<div id="div">
<ponent [showLast]="isPrint"></ponent>
</div>
print() {
const selectionHtml: HTMLElement = document.getElementById('div');
this.isPrint = true; //Boolean I used to change the ponent inside the div
selectionHtml.onload
setTimeout(() => {
PrintUtil.print('page');
this.isPrint = false;
selectionHtml.onload;
}, 100);
}
本文标签: javascriptAngular How to refresh a part of html ( formdivtable )Stack Overflow
版权声明:本文标题:javascript - Angular: How to refresh a part of html ( formdivtable ) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741423954a2377966.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论