我正在使用 Angular 版本 7 開發一個 angular 項目。我有一個顯示一些 json 對象的表。在每個 json 對象中,我添加了 2 個按鈕,當每個按鈕被選中時,我都會有條件地顯示這些按鈕。我的意思是,當我單擊“編輯”按鈕時,我希望僅在我按下按鈕的那一行顯示“無編輯”按鈕,反之亦然。但是發生的情況是,當我單擊特定行的“編輯”按鈕時,其他行的所有其他按鈕都會更改。我怎樣才能做到這一點?myapp.component.html文件:<table> <thead> <tr> <th scope="col" translate>Name</th> <th scope="col" translate>Description</th> </tr> </thead> <tbody> <tr *ngFor=" let product of products"> <td>{{product.name}}</td> <td>{{datasource.description}}</td> <td> <button *ngIf="allowEdit" (click)="edit(product)"> Edit </button> <button *ngIf="allowNoEdit" (click)="noEdit(product)"> No Edit </button> </td> </tr> </tbody> </table>myapp.component.ts文件:allowEdit: boolean = true;allowNoEdit: boolean = false;edit(product) { this.allowEdit= !this.allowEdit; this.allowNoEdit= !this.allowNoEdit;}noEdit(product) { this.allowEdit= !this.allowEdit; this.allowNoEdit= !this.allowNoEdit;}
ngIf 中的條件如何僅對所選行為真
UYOU
2021-06-28 00:20:23