0
I’m developing an online ordering app using Ionic 4.
This app has a benefits system. When the user opens the shopping cart he can open a modal where your available benefits are listed.
When the user selects the benefit within the modal, this benefit should automatically appear in the cart, when the modal is closed.
cart.pagets.
...
beneficio:any
...
async mostraBeneficios(){
const modal = await this.modalController.create({
component: BeneficiosComponent,
});
return await modal.present();
}
...
carregaBeneficioSelecionado(){
this.beneficio = this.beneficioService.getBeneficios()
console.log(this.beneficio)
}
...
cart.page.html
...
<!-- Botao que mostra modal -->
<ion-buttons slot="end">
<ion-button (click)="mostraBeneficios()">
<ion-icon icon="../assets/icon/tag.svg"></ion-icon>
Beneficios
</ion-button>
</ion-buttons>
...
<!-- Item que deve mostrar o beneficio após ser selecionado -->
<ion-item *ngIf="beneficio">
<ion-label> {{ beneficio.nome }} </ion-label>
</ion-item>
...
Modal: benefits.component.ts
constructor(
private modalController: ModalController,
private firebaseProvider: FirebaseProvider,
private beneficiosService: BeneficiosService
) {
this.carregaBeneficios();
}
fechaModal(){
this.modalController.dismiss();
}
...
beneficios:any
...
// lista os beneficios disponiveis
carregaBeneficios(){
this.firebaseProvider.getBeneficios()
.then((r) => {
this.beneficios = r
})
}
//captura o beneficio selecionado
radioBeneficio($event){
this.beneficiosService.setBeneficios($event.detail)
}
benefits.component.html
<ion-content>
<ion-list>
<ion-radio-group name="auto">
<ion-card *ngFor="let b of beneficios">
<ion-card-header>
<ion-item (ionSelect)="radioBeneficio($event)" lines="none">
<ion-label text-left> {{ b.descricao }} </ion-label>
<ion-label text-right>
<ion-badge color="primary" mode="ios"> {{ b.pontos }} </ion-badge>
<p>pontos</p>
</ion-label>
<ion-radio [value]="b" color="success" slot="end"></ion-radio>
</ion-item>
</ion-card-header>
</ion-card>
</ion-radio-group>
</ion-list>
</ion-content>
<ion-footer class="footer">
<ion-button class="btn-footer" fill="clear" expand="full" (click)="fechaModal()">
Cancelar
</ion-button>
</ion-footer>
How can I update the <ion-item> in the cart.page.html automatically When the modal is closed?
With the Observable worked perfectly, was my initial idea, but did not know how to implement in the code. Like @Input, an error was displayed in the console when clicking on the benefit:
Input is not a function. But the problem was solved with the Observable, thank you.– Renan Cesar de França
The error was pq I in the example added callbackInput and not callbackBeneficios. But with observable working successfully !!
– Eduardo Araya Jezine