【发布时间】:2021-12-05 03:53:38
【问题描述】:
除了 [(ngModel)] 之外,我对使用什么感到有点困惑。正如您在提供的代码 sn-p 和图像下方可以看到的那样,由于 *NgFor,当我单击编辑按钮时,ngModel 绑定将应用于所有其他输入字段。请各位大神给我一些建议好吗?
Below is cart component template.
<div class="container my-5">
<h1>Your Cart</h1>
<div *ngFor="let item of listAlbums" class="container">
<div class="row">
<div class="col-md-4">
<h6>Item name: {{item.title}}</h6>
<img src="{{item.thumbnailUrl}}">
<input class="form-control" type="number" value="{{item.quantity}}">
<button (click)="remove(item)" class="btn btn-danger mx-3">X</button>
<button (click)="edit(item)" class="btn btn-warning">Edit</button>
<input type="text" placeholder="{{item.title}}" [(ngModel)]="newTitle" [hidden]="!hideEdit">
</div>
</div>
</div>
Below is the cart component.
@Component({
selector: 'app-cart',
templateUrl: './cart.component.html',
styleUrls: ['./cart.component.scss']
})
export class CartComponent implements OnInit {
listAlbums:Album[]=[]
hideEdit:boolean=false
newTitle:string;
constructor(private productService:ProductService) {
}
ngOnInit() {
this.productService.itemList.subscribe(data=>
{this.listAlbums = data })
}
remove(item:Album) {
this.productService.removeItem(item)
}
edit(item:Album) {
this.hideEdit = !this.hideEdit
this.productService.editItem(item, this.newTitle)
}
}
【问题讨论】:
标签: javascript html angular typescript