我要做的是在每一行上设置一个属性来指示它是否正在被编辑,如下所示:
https://stackblitz.com/edit/angular-usbhmd
不想点击的人的代码。
HTML
<table class="table table-hover">
<thead>
<tr>
<th>Domain</th>
<th>Registered Date</th>
<th>Action</th>
</tr>
<tr *ngFor="let domain of domains; let i = index">
<td>
<span *ngIf="!domain.editable">{{domain.name}}</span>
<input type="text" class="form-control" [(ngModel)]="domain.name" *ngIf="domain.editable"/>
</td>
<td>
<span *ngIf="!domain.editable">{{domain.reg_date}}</span>
<input type="date" class="form-control" [(ngModel)]="domain.reg_date" *ngIf="domain.editable"/>
</td>
<td><button class="btn btn-primary" (click)="editDomain(domain)">Edit</button></td>
</tr>
</thead>
<tbody>
</tbody>
</table>
组件
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
domains = [];
isVisible : boolean = false;
constructor(){
this.domains = [
{
"_id" : "12323vdfvd234",
"name" : "sixlogs.com",
"reg_date" : "2018-04-04",
"editable": false
},
{
"_id" : "12323vdfvd234",
"name" : "avanza.com",
"reg_date" : "2019-04-04",
"editable": false
},
{
"_id" : "12323vdfvd234",
"name" : "tps.com",
"reg_date" : "2018-04-04",
"editable": false
},
{
"_id" : "12323vdfvd234",
"name" : "utf.com",
"reg_date" : "2019-04-04",
"editable": false
}
]
}
editDomain(domain: any){
domain.editable = !domain.editable;
}
}
如您所见,我已将editable 属性添加到domains 数组中,当按钮单击触发editDomain 时,该属性会为对象设置
事件。使用editable 属性,您可以更改视图以显示每一行的输入。