Ejemplo 1: compartir datos entre componentes angulares
import { Component, Input } from '@angular/core';
//Child-Component
@Component({
selector: 'app-child',
template: `
Say {{ message }}
`,
styleUrls: ['./child.component.css']
})
export class ChildComponent {
@Input() childMessage: string;
constructor() { }
}
======================================================================
import { Component } from '@angular/core';
//Parent-Component data transfer to Child-Component
@Component({
selector: 'app-parent',
template: `
<app-child [childMessage]="parentMessage"></app-child>
`,
styleUrls: ['./parent.component.css']
})
export class ParentComponent{
parentMessage = "message from parent"
constructor() { }
}
Ejemplo 2: compartir datos usando el servicio angular 6
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
@Injectable()
export class DataService {
private messageSource = new BehaviorSubject('default message');
currentMessage = this.messageSource.asObservable();
constructor() { }
changeMessage(message: string) {
this.messageSource.next(message)
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)