Goal: Set up a service to display the input value entered in input filed.
-
input field
-
<display></display>
: to display output -
ngModel: property biding - updating DOM element
-
ngModelChange: event binding - notifies changes in DOM
import { DisplayService }
private unsubscribe = new Subject<void>();
- ngOnInit(): getInput
- ngOnDestroy(): unsubscribe
- updateService: setInput
export class AppComponent implements OnInit, OnDestroy {
public name: string;
private unsubscribe = new Subject<void>();
constructor(private displayer: DisplayService) { }
public ngOnInit(): void {
this.displayer
.getInput()
.pipe(takeUntil(this.unsubscribe))
.subscribe(input => {
this.name = input;
});
}
public ngOnDestroy(): void {
this.unsubscribe.next();
this.unsubscribe.complete();
}
public updateService(value: string) {
this.displayer.setInput(value);
}
}
declarations: [DisplayComponent]
: register DisplayComponent
<div>{{ display }}</div>
import { DisplayService }
private unsubscribe = new Subject<void>();
- ngOnInit(): getInput
- ngOnDestroy(): unsubscribe
- BehaviorSubject
- public getInput()
- public setInput
export class DisplayService {
private display: BehaviorSubject<string>;
constructor() {
this.display = new BehaviorSubject<string>('Enter here...'); // default value as 'Enter here...'
}
public getInput(): Observable<string> {
return this.display.asObservable();
}
public setInput(input: string): void {
this.display.next(input);
}
}