[英]How to clear the dropdowns values when i navigate to back page in Angular 6
Image In App Component I am having the dropdownlist, IF I select any option in the dropdown it should navigate to another page.And I have done this through routing.图片在应用程序组件中我有下拉列表,如果我 select 下拉列表中的任何选项它应该导航到另一个页面。我已经通过路由完成了这个。 when I click on back button it should navigate me to the app component.当我单击后退按钮时,它应该将我导航到应用程序组件。 I also did this.. But the problem is when I navigate to back, I selected dropdwon value is still present there..我也这样做了..但问题是当我导航到后面时,我选择的 dropdwon 值仍然存在于那里..
export class AppComponent {
data;
constructor(private router:Router){}
list =['prodcuts','myorder'];
selectOption(value){
this.data=value;
if(value==='prodcuts'){
this.router.navigate(['/products']);
}
}
<div class="container">
<label>Summary: </label>
<select (change)="selectOption($event.target.value)">
<option value="0">--All--</option>
<option *ngFor="let summary of list" value={{summary}}>
{{summary}}
</option>
</select>
</div>
<router-outlet></router-outlet>
<button class="btn btn-info" routerLink="">Back</button>
Im guessing the reason it is not resetting is because your router-outlet
is inside off your app component where the drop-down is.我猜它没有重置的原因是因为您的router-outlet
位于下拉列表所在的应用程序组件内部。 Therefore your app component persists when you change page and the data of what has been selected will also persist.因此,当您更改页面时,您的应用程序组件会持续存在,并且所选内容的数据也将持续存在。 I imagine you want this so you can display it above the page you navigate to within the router outlet.我想你想要这个,所以你可以在路由器插座内导航到的页面上方显示它。 As i cannot see your routing i cannot tell 100% if this is the case, but if it is please see below.由于我看不到您的路由,因此我无法 100% 判断是否是这种情况,但如果是,请参见下文。
One way you could tackle this would be to wrap the select in a reactive form.解决此问题的一种方法是以反应形式包装 select。 You could then fire an event when the user clicks back and reset the form.然后,当用户单击并重置表单时,您可以触发一个事件。
<form [formGroup]="form">
<select formControlName="pageSelect"></select>
</form>
<button class="btn btn-info" (click)="back()">Back</button>
form = this._fb.group({
pageSelect: ['']
})
constructor(
private _fb: FormBuilder
) {
}
back() {
this.router.navigate(['']);
this.form.reset();
}
You could make a reset function and use it on ngOnDestroy()
您可以重置 function 并在ngOnDestroy()
上使用它
reset() {
var dropDown = document.getElementById("myDropdown");
dropDown.selectedIndex = 0;
}
ngOnDestroy(){
this.reset();
}
A router outlet emits a deactivate event when a component is destroyed.当组件被销毁时,路由器出口会发出 deactivate 事件。 You can modify your router-outlet
like -您可以修改您的router-outlet
,如 -
<router-outlet
(deactivate)='onDeactivate($event)'></router-outlet>
and then write resetting logic in onDeactivate
function.然后在onDeactivate
function中写入复位逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.