简体   繁体   English

当我导航到 Angular 6 中的后页时如何清除下拉列表值

[英]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.

相关问题 Angular 2 Jquery插件 - 导航然后返回页面时出现问题 - Angular 2 Jquery Plugin - Issue when navigate away then back to page 使用角度 11 导航回上一页时如何保持滚动到的位置? - How to keep position which scrolled to when navigate back to previous page with angular 11? 如何在Angular7的不同选项卡中导航回上一个路线/页面? - How to navigate back to previous route/page in different tab in Angular7? 使用 Angular 按下后退按钮时如何导航到另一条路线 - How do I navigate to another route when back button is pressed with Angular Angular 在不重新加载页面的情况下向后导航 - Angular navigate back without reloading the page Angular4——从当前页面返回时上一页不刷新 - Angular4 --previous page doesn't refresh when navigate back from current page 如何在Angular中导航(重定向)到我想要的任何页面? - How do I navigate(redirect) to any page I want in Angular? 当使用浏览器后退按钮导航到页面时,角度绑定在Chrome上的应用不正确 - Angular bindings applied incorrectly on Chrome when browser back button is used to navigate to page 如何以角度 6 导航到其他页面? - How to navigate to other page in angular 6? Ionic 4 Angular 路由器导航并清除上一页的堆栈/历史记录 - Ionic 4 Angular router navigate and clear stack/history of previous page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM