Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: pass parameter to another component

I'm stuck with Angular2, I would like to pass parameters from a Products page (eg: product ids) to a Payment page, here is what I tried so far:

enter image description here

payment.html :

  Message: {{message}}
  <page-products></page-products>

payment.ts :

import { Component, ViewChild, AfterViewInit} from '@angular/core';
import { ProductsPage } from "../../products/products";

@IonicPage()
@Component({
    selector: 'page-payment',
    templateUrl: 'payment.html'
})
export class PaymentPage implements AfterViewInit {

     @ViewChild(ProductsPage) child;

     constructor(public navCtrl: NavController) {}

     message:string;

     ngAfterViewInit() {
             this.message = this.child.message
     }
    }

products.ts :

import { Component} from '@angular/core';
import { IonicPage, NavController } from 'ionic-angular';

@IonicPage()
@Component({
    selector: 'page-products',
    templateUrl: 'products.html',
})
 export class ProductsPage {

     message: string = "Hola Mundo!"

     constructor(public navCtrl: NavController) {}

     goToPayment() {
         this.navCtrl.setRoot('PaymentPage');
     }
 }

product.html :

<button ion-button (click)="goToPayment()">pay</button>

but I always get this error message:

enter image description here

Any idea of what I'm doing wrong?


here is my payment.module.ts:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ComponentsModule } from '../../../components/components.module';
import { PaymentPage } from './payment';
import { ProductsPage } from '../../products/products';

@NgModule({
   declarations: [
       PaymentPage, ProductsPage  <-- if I add here ProductsPage, then new error (see under)
   ],
   imports: [
        IonicPageModule.forChild(PaymentPage),
        ComponentsModule
   ]
})
export class PaymentPageModule {}

and products.module.ts :

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ProductsPage } from './products';
import { ComponentsModule } from '../../components/components.module';

@NgModule({
   declarations: [
      ProductsPage
   ],
   imports: [
      IonicPageModule.forChild(ProductsPage),
      ComponentsModule
  ]
})
export class ProductsPageModule {}

and if I declare ProductsPage like above then I get this error message:

enter image description here

like image 488
Julien Avatar asked Jun 09 '26 17:06

Julien


1 Answers

To pass parameters between components that dont have a hierarchical relationship you should use a service. To achieve this, first create the service:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

@Injectable()
export class MyService {

  private messageSource = new BehaviorSubject<type_of_the_message>(message);
  currentMessage = this.messageSource.asObservable();

  constructor() { }

  sendMessage(message: type_of_the_message) {
    this.messageSource.next(message);
  }

}

Then in the sender component:

    import { Component, OnInit } from '@angular/core';
    import { MyService } from 'pathToService/MyService.service';

    @Component({
      selector: 'sender',
      templateUrl: 'senderTemplate',
      styleUrls: ['senderStyles']
    })
    export class SenderComponent implements OnInit {

      private message: type_of_the_message;

      constructor(private myService: MyService) {}

      ngOnInit() {
        this.myService.currentMessage.subscribe(message => this.message = message);
      }

      sendMessage() {
        this.myService.sendMessage(!this.message);
      }
    }

And finally in the receiver component:

import { Component, OnInit } from '@angular/core';
import { MyService } from 'pathToService/MyService.service';

@Component({
  selector: 'receiver',
  templateUrl: 'receiverTemplate',
  styleUrls: ['receiverStyles']
})
export class ReceiverComponent implements OnInit {

  private message: boolean;

  constructor(private myService: MyService) {}

  ngOnInit() {
    this.myService.currentMessage.subscribe(message => this.message = message);
  }
}
like image 185
Juan Avatar answered Jun 12 '26 12:06

Juan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!