简体   繁体   English

在angular7中第二个选择网址中没有更改表单值

[英]no change form value in second select url in angular7

I have a list of categories and I need when I click on the edit button, open the edit form and put the value of that category to edit them. 我有一个类别列表,当我点击编辑按钮,打开编辑表单并输入该类别的值进行编辑时,我需要这些类别。

when I click for the first time its ok and work good but in second click URL change but the value of form not change and need to refresh the page for change value. 当我第一次点击它确定并且工作正常但是在第二次点击URL更改但是表单的值没有改变并且需要刷新页面以获得更改值。

Sample Code 示例代码

this is my routing code : 这是我的路由代码:

{
    path: 'categroies', component: ManagegategoriesComponent, children: [
      { path: '', component: AddcategoriesComponent },
      { path: 'edit/:id', component: EditcategoryComponent }
    ]
  }

this html code : 这个HTML代码:

    <div class="CatrgoryTitle col-md-12 col-x-12 col-sm-12 col-lg-12 mt-1">
        <router-outlet></router-outlet>
</div>
<div class="CatrgoryTitle col-md-12 col-x-12 col-sm-12 col-lg-12 mt-4">
        <nz-table #basicTable [nzData]="listOfData" class="table table-responsive">
            <thead>
                <tr>
                    <th>نام دسته</th>
                    <th>عملیات</th>
                </tr>
            </thead>
            <tbody>
                <tr *ngFor="let item of basicTable.data">
                    <td>{{item.name}}</td>
                    <td>
                            <button class="btn btn-warning"
                                routerLink="/panel/dashboard/categroies/edit/{{item.id}}">ویرایش</button>
                    </td>
                </tr>
            </tbody>
        </nz-table>
</div>

what's the problem ? 有什么问题 ? how can I solve this problem?? 我怎么解决这个问题??

Might be the problem because of routerLink , because on first click you will redirected on edit page and when you again click then the url will not be refresh, 可能是因为routerLink问题,因为在第一次点击时你将在编辑页面上重定向,当你再次点击时,网址将不会刷新,

Try below solution for this, 尝试以下解决方案,

Use router.Navigate() method for this router.Navigate()使用router.Navigate()方法

In .ts, 在.ts,

import { Router } from '@angular/router';

constructor(
        private router: Router
    ) { }

onRedirectToEdit(item){
   this.router.navigate(['/panel/dashboard/categroies/edit', item.id]);
}

In Html, 在Html中,

<td>
    <button class="btn btn-warning" (click)="onRedirectToEdit(item)">ویرایش</button>
</td>

in edit.ts edit.ts

ngOnInit() {
     this.route.params.subscribe(params => {
      if (params && params['id']) {
        this.id = +params['id'];
      }
    });
  }

As per you have given stackblitz . 根据你给stackblitz

You need to subscribe the router parameters on each change 您需要在每次更改时订阅路由器参数

this.route.paramMap.subscribe(res=> {
   this.id = res.get('id');
});

you must use this code in constructor : 你必须在构造函数中使用此代码:

private router:ActivatedRoute

 this.router.params.subscribe(routeParams => {
   /// load data
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM