<ion-card *ngFor="let product of products" no-padding>
<ion-item>
<ion-select [(ngModel)]="qty" *ngIf="product.type==variable" [disabled]="false">
<ion-option value="1">1kg</ion-option>
<ion-option value="250">250gm</ion-option>
<ion-option value="500">500gm</ion-option>
<ion-option value="100">100gm</ion-option>
</ion-select>
</ion-item>
<button [disabled]=disable color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src)">ADD</button>
</ion-card>
这是我的
var disable=false;
addlist(name, id, price, qty, image) {
//it is disabling each button present in the list
this.disable=true;
}
答案 0 :(得分:0)
在您的组件中,根据您所需的loop
制作products
disable
并添加condition
属性:
for (let product of products) {
product.disabled = false;
if(product.somefield == 'somevalue'){
product.disabled = true;
}
}
在HTML 中将button disabled
属性设为[disabled]="product.disabled"
<button [disabled]="product.disabled" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src)">ADD</button>
答案 1 :(得分:0)
如果您对阵列中的每件商品使用相同的disable
属性,那么问题是什么呢?您需要为每件商品使用特定的disable
你的阵列。
我看到两种方法:
products
并添加disable
属性。如果它来自服务器并且您可以并且想要,那么您可以在那里添加该属性,这样您就不会浪费一点时间在前端执行此操作。disable
的新数组,并使用* ngFor索引访问每个项目的正确禁用。在您的.ts文件中,您将拥有:
// Let's say you're fetching your products in a lifecyclehook like ionViewDidLoad
ionViewDidLoad = () => {
// your call for the api...
myHttpCall.then(prods => {
this.products = prods;
// let's use map to iterate through the products
this.products.map(item => {
// this'll create a property in every item of your array.
item.disabled = false;
})
})
}
addlist(name, id, price, qty, image, product) {
// you'll need to pass the entire object to your method, and then set the property to true.
product.disabled = true;
}
我本可以直接在响应中使用map,或者使用扩展运算符,但我们的目标是最简单的。
在你的HTML中:
<!-- All your card code. Change the disabled of the button and the select as bellow -->
<button [disabled]="product.disabled" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src, product)">ADD</button>
<!-- Just pass the entire product in the end of the method -->
在.ts中,您将创建一个新数组,并且对于产品中的每个项目,您都会推送一个新的布尔值:
export class YourPage {
// the new boolean array
public disabled: Array<boolean> = [];
// The method where you get the products, same as the first option
ionViewDidLoad = () => {
// your call for the api...
myHttpCall.then(prods => {
this.products = prods;
// use a for to iterate through
for (let i = 0; i < this.products.length; i++){
this.disabled.push(false);
}
})
}
addlist(name, id, price, qty, image, index) {
// now you'll pass the index from the ngfor
// you'll access the corresponding disable from the array and set to true
this.disabled[index] = true;
}
}
然后在你的html:
<!-- declare the index in your ngFor -->
<ion-card *ngFor="let product of products; let i = index" no-padding>
<ion-item>
<!-- change the disable to reflect the correct array position -->
<ion-select [(ngModel)]="qty" *ngIf="product.type==variable" [disabled]="disabled[i]">
<ion-option value="1">1kg</ion-option>
<ion-option value="250">250gm</ion-option>
<ion-option value="500">500gm</ion-option>
<ion-option value="100">100gm</ion-option>
</ion-select>
</ion-item>
<!-- Add the index in the end of your click method -->
<button [disabled]="disabled[i]" color="light" class="addbtn" ion-button clear (click)="addlist(product.name,product.id,product.price*product.quantity,product.quantity,product.images[0].src, i)">ADD</button>
</ion-card>
希望这会有所帮助:D