I have two components basically. The first one contains Angular Material option (mat-option) to select the option from combobox. The second one is supposed to show the selected data. I have created a shared Service using Subject with the hope of passing it between them but it's no luck.
The Service
@Injectable()
export class ToothpasteService {
toothpasteName: Subject<string>;
toothpasteName$: Observable<any>;
constructor() {
this.toothpasteName = new Subject();
this.toothpasteName$ = this.toothpasteName.asObservable();
}
}
First Component (ToothpasteComponent.ts)
export class ToothpasteComponent implements OnInit {
toothpastes: Toothpaste[] = [
{value: 'to-0', viewValue: 'Toothpaste A'},
{value: 'to-1', viewValue: 'Toothpaste B'},
{value: 'to-2', viewValue: 'Toothpaste C'},
]
constructor(private toothpasteService: ToothpasteService) { }
onToothpasteChanged(toothpasteValue: any) {
this.toothpasteService.toothpasteName.next(toothpasteValue);
}
ToothpasteComponent.html
<div class="container">
<mat-form-field>
<mat-label>Select your toothpaste: </mat-label>
<mat-select>
<mat-option *ngFor="let toothpaste of toothpastes" [value]="toothpaste.viewValue" (onSelectionChange)="onToothpasteChanged(toothpaste.viewValue)">
{{toothpaste.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</div>
Second Component (HeaderComponent.ts)
export class HeaderComponent implements AfterViewChecked {
toothpasteName: string;
constructor(private toothpasteService: ToothpasteService) {
}
ngAfterViewChecked() {
this.toothpasteService.toothpasteName$.subscribe((toothpasteData)=>{
this.toothpasteName = toothpasteData;
})
}
}
HeaderComponent.html
<div class="justify-content-start align-items-start align-self-center">
<p>Toothpaste Selected: {{toothpasteName}}</p>
</div>
This does not work. Am I missing something?