I have two component in my angular 4 application and i want to share values between them.
the first component has a dropdown button which ,when selecting an item,allows me to send data to another component to print it.
i wrote this code the service
import { Injectable } from '@angular/core';
@Injectable()
export class DataService {
private message : String ;
sendMessage(message: string) {
this.message=message;
}
getMessage(): String {
return this.message;
}
}
the component wich has the dropdown button
export class ToolbarDropdownComponent implements OnInit {
message: any ;
constructor(private loginservice:LoginService,private dataService: DataService) { }
ngOnInit() {
sendMessage(): void {
this.dataService.sendMessage(this.message);
}
}
the other component
constructor(
private dataservice: DataService
) { }
ngOnInit() {
console.log("emitted message is"+this.dataservice.getMessage());
}