One of my services involves setting an ID, with the ID itself being obtained from another component through a click event.
export class MyService {
id: number;
constructor() {}
public setId(value: number) {
this.id = value;
console.log('id:', this.id);
}
}
The process of setting the value occurs within a click event located in my menu component:
<a [routerLink]="['item.id]" (click)="getId(item.id);">
This click event triggers a method within the menu component:
getId(datavalue: number)
{
this.ms.setId(datavalue);
}
Now I need to pass this value to another component so that it can be displayed in HTML. How can this be achieved?
class MyComponent implements OnInit
{
id: number;
constructor(private ms: MyService)
{
this.id = ms.id;
console.log('ms id = ', this.id);
}
Even though I attempted the approach above, when checking the console log, the output shows "undefined".