Angular:- value defined in one component use it in another component using services

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Angular:- value defined in one component use it in another component using services



I created service to share data between two components, I read documentation and checked few articles but couldn't understand it properly and stuck.



I created the stackblitz to learn as a sample for data sharing. Here's the link
stackblitz



So below is my code
footer.component.ts


//trying to set value here
test()
let height:any;
this.footerHeightService.setFooterHeight(height)
height = document.getElementById("footer").offsetTop;



footer-height.service.ts


import Injectable from '@angular/core';


@Injectable()
export class FooterHeightService


footerOffset:any;
constructor()

setFooterHeight(height)
this.footerOffset = height;


getFooterHeight(height)
return this.footerOffset;





app.component.ts


//trying to get value here
constructor(private footerHeightService:FooterHeightService)

test()
let height:any;
this.footerHeightService.getFooterHeight(height);
console.log("this.footerHeightService.getFooterHeight(height);", height);



ngOnInit():void
this.test();





You can utilise input , output modules
– Akhil Aravind
Aug 10 at 4:01


input


output





its not parent child components. I think input, output is used for parent and child components
– Developer
Aug 10 at 4:04






You are including footer in all the component, so why cant you use input , output
– Akhil Aravind
Aug 10 at 4:16


input


output





It was a sample to understand the data sharing using devices in stuck in my project
– Developer
Aug 10 at 4:23




1 Answer
1



First You need to create a service


import Injectable from '@angular/core';

@Injectable()
export class SharedService

constructor()
dataShared: any

setFooterData(data)
this.dataShared = data

getFooterData()
return this.dataShared





And in your component footer, you will send the data to the service by calling its method set


constructor(
...
private sharedService: SharedServiceService
)

componentMethod()
this.sharedService.setFooterData(data)



And in the component app, you will get the data by calling the get method


constructor(
...
private sharedService: SharedServiceService
)

componentMethod()
this.sharedService.getFooterData()



live code





Thanks for the clarification 😀
– Developer
Aug 10 at 4:24





ca you correct what I am doing wrong?
– Developer
Aug 10 at 14:10





In your test method of your footer component, you're setting an undefined data height. Consider assigning a value to it before setting the value of the service. It should be height = document.getElementById("footer").offsetTop; this.footerHeightService.setFooterHeight(height). Also, please see the way I defined the get method of the service without parameter
– edkeveked
Aug 10 at 18:14



test


height


height = document.getElementById("footer").offsetTop; this.footerHeightService.setFooterHeight(height)





can you check I updated it I followed tour instructions seems still missing something
– Developer
Aug 10 at 18:29






What is the error you have?
– edkeveked
Aug 10 at 18:34






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS