1import { Component, Input } from '@angular/core';
2//Child-Component
3@Component({
4 selector: 'app-child',
5 template: `
6 Say {{ message }}
7 `,
8 styleUrls: ['./child.component.css']
9})
10export class ChildComponent {
11
12 @Input() childMessage: string;
13
14 constructor() { }
15
16}
17======================================================================
18import { Component } from '@angular/core';
19//Parent-Component data transfer to Child-Component
20@Component({
21 selector: 'app-parent',
22 template: `
23 <app-child [childMessage]="parentMessage"></app-child>
24 `,
25 styleUrls: ['./parent.component.css']
26})
27export class ParentComponent{
28 parentMessage = "message from parent"
29 constructor() { }
30}
1import { Injectable } from '@angular/core';
2import { BehaviorSubject } from 'rxjs';
3
4@Injectable()
5export class DataService {
6
7 private messageSource = new BehaviorSubject('default message');
8 currentMessage = this.messageSource.asObservable();
9
10 constructor() { }
11
12 changeMessage(message: string) {
13 this.messageSource.next(message)
14 }
15
16}
17