因此,我没有任何代码可向您显示。我想保存一个位于数组内部的对象键(id)。当我单击ngFor
项目之一时,将其保存在变量中。如果我单击另一个项目,则将包含先前ID的变量与我选择的实际ID进行比较。你们可以帮我吗?
async trocarMercado(mercado) {
this.mercado = mercado;
console.log(this.mercado);
if (this.mercadoAnterior != this.mercado.id) {
this.mercadoAnterior = this.mercado.id;
}
console.log(this.mercadoAnterior)
if (this.carrinho.length > 1) {
const alert = await this.alertCtrl.create({
header: 'Atenção',
message: 'Se entrar num novo mercado vai perder o carrinho do mercado anterior. Tem a certeza?',
buttons: [{
text: 'Sim',
handler: () => {
this.carrinho.length = 0;
this.navCtrl.navigateRoot(['/produtos']);
}
}, {
text: 'Não',
handler: () => {
}
}]
});
await alert.present();
}
}
答案 0 :(得分:0)
如果您将其存储在clickedItem
中,并假设您拥有*ngFor
,如下所示`:
<li *ngFor="let item of items" ><a class="dropdown-item btn-sm" (click)="eventClick(item)" >{{ item.title }}</a></li>
现在在您的组件中:
eventClick(item: any) {
if(item === this.clickedItem) {
console.log('same item clicked nothing to do here');
} else {
this.clickedItem = item;
console.log('different item clicked , store it now');
}
}
答案 1 :(得分:0)
解决任何有类似问题的人。
if(mercado.id == this.clickedId || this.clickedId == undefined){
this.mercadoVer=true;
this.clickedId= mercado.id;
const loading = await this.loadCtrl.create({
message: 'Aguarde...',
duration: 500
});
await loading.present();
this.navCtrl.navigateRoot(['/produtos'])
}else{
const alert = await this.alertCtrl.create({
header: 'Atenção',
message: 'Se entrar num novo mercado vai perder o carrinho do mercado anterior. Tem a certeza?',
buttons: [
{
text: 'Sim',
handler: async () => {
this.carrinho.length= 0;
this.clickedId= mercado.id;
const loading = await this.loadCtrl.create({
message: 'Aguarde...',
duration: 500
});
await loading.present();
this.navCtrl.navigateRoot(['/produtos']);
}
}, {
text: 'Não',
handler: () => {
}
}
]
});
await alert.present();