How can I use a service to initialize a variable once on app.module and use it later in other components?


Thieb

I have to generate a list of objects once the app starts using the service from the backend API, the question is how to initialize the list with other components and access it?

For example, in app.module.ts I want something like:

    export class AppComponent {
  title = 'General Title';
  myListFromServiceApi:[DataType]; 
  }

How to call it in mycomponent.ts

ngOnInit() {
this.myListFromServiceApi= ??
}

edit

I already have the service, but I want to avoid calling the service every time a component is added to the view, i.e. once in the life of the application if possible, how can I achieve this?

Jan Wendland

You can use the rx operator to prevent multiple executions of side effects (http requests in your case) when subscribing to multiple subscribers, and return an observable that is piped from the service , ie :shareshareDataTypeService

export class DataTypeService {

  dataTypes$: Observable<DataType>;

  constructor(private http: HttpClient) {
    this.dataTypes$ = this.http.get('/api/datatypes').pipe(share());
  }

  getDataTypes() {
    return this.dataTypes$;
  }
}

In your component you can inject DataTypeService, subscribe to the returned observable getDataTypes()and the request will only be executed once.

constructor(private dataTypeService: DataTypeService){
   this.dataTypeService.getDataTypes().subscribe(dataTypes => {
       this.myListFromServiceApi = dataTypes;
   })
}

If you only want to use these data types in templates, you can also store the returned observable directly getDataTypes()on the component and take advantage of the pipelineasync

Related


How can I initialize a variable to use it later in PHP?

wheat I want to initialize a variable and want to use it later in my foreach loop, here is what I do: $lastdepdate = ""; $resultDEP = "SELECT * FROM mytable"; foreach($resultDEP as $rows){ $lastdepdate = trim($row['LastDepDate']); } I get the d

How can I initialize a variable to use it later in PHP?

wheat I want to initialize a variable and want to use it later in my foreach loop, here is what I do: $lastdepdate = ""; $resultDEP = "SELECT * FROM mytable"; foreach($resultDEP as $rows){ $lastdepdate = trim($row['LastDepDate']); } I get the d

How can I initialize a variable to use it later in PHP?

wheat I want to initialize a variable and want to use it later in my foreach loop, here is what I do: $lastdepdate = ""; $resultDEP = "SELECT * FROM mytable"; foreach($resultDEP as $rows){ $lastdepdate = trim($row['LastDepDate']); } I get the d

How can I initialize a variable to use it later in PHP?

wheat I want to initialize a variable and want to use it later in my foreach loop, here is what I do: $lastdepdate = ""; $resultDEP = "SELECT * FROM mytable"; foreach($resultDEP as $rows){ $lastdepdate = trim($row['LastDepDate']); } I get the d

How can I save a variable in a loop to use it again later?

pB2556: I want to make a program that puts the ellipses next to each other until one hits the border of the canvas and then goes the other way. Unfortunately it only works in one direction and stops when it hits the right border. Is it possible to save the var

How can I save a variable in a loop to use it again later?

pB2556: I want to make a program that puts the ellipses next to each other until one hits the border of the canvas and then goes the other way. Unfortunately it only works in one direction and stops when it hits the right border. Is it possible to save the var

How can I save a variable in a loop to use it again later?

pB2556: I want to make a program that puts the ellipses next to each other until one hits the border of the canvas and then goes the other way. Unfortunately it only works in one direction and stops when it hits the right border. Is it possible to save the var

How can I save a variable in a loop to use it again later?

pB2556: I want to make a program that puts the ellipses next to each other until one hits the border of the canvas and then goes the other way. Unfortunately it only works in one direction and stops when it hits the right border. Is it possible to save the var

How can I store the query name in a variable and use it later?

Volume 1 My page returns two queries based on the page you are viewing: QueryA和QueryB I determine which page to look at, then I want to dynamically change the CF code to use the correct query name (pseudocode example): <cfif GetBaseTemplatePath() EQ #ExpandPat

How can I save a variable in a loop to use it again later?

pB2556: I want to make a program that puts the ellipses next to each other until one hits the border of the canvas and then goes the other way. Unfortunately it only works in one direction and stops when it hits the right border. Is it possible to save the var

How can I save a variable in a loop to use it again later?

pB2556: I want to make a program that puts the ellipses next to each other until one hits the border of the canvas and then goes the other way. Unfortunately it only works in one direction and stops when it hits the right border. Is it possible to save the var

How can I declare a variable in a component for later use?

black I have a component whose variables are not assigned until ngAfterViewInit. I also want to use these variables outside init, but I don't know how. (below is what I thought, but it gives an error saying type 'map' is not assignable to type 'undefined')

How can I store the query name in a variable and use it later?

Volume 1 My page returns two queries based on the page you are viewing: QueryA和QueryB I determine which page to look at, then I want to dynamically change the CF code to use the correct query name (pseudocode example): <cfif GetBaseTemplatePath() EQ #ExpandPat

How can I use my value-added in other components

Patrick Van Newburg For school I need to make a react app, this is my first time using html, css, javascript and react and now I am stuck at the part where I have to post data to the API. What I'm trying to do is create a page where people can choose to eat an

How can I use my value-added in other components

Patrick Van Newburg For school I need to make a react app, this is my first time working with html, css, javascript and react and now I am stuck at the part where I have to post data to the API. What I'm trying to do is create a page where people can choose to

How can I use my value-added in other components

Patrick Van Newburg For school I need to make a react app, this is my first time working with html, css, javascript and react and now I am stuck at the part where I have to post data to the API. What I'm trying to do is create a page where people can choose to

How can I use my value-added in other components

Patrick Van Newburg For school I need to make a react app, this is my first time working with html, css, javascript and react and now I am stuck at the part where I have to post data to the API. What I'm trying to do is create a page where people can choose to

How can I use my value-added in other components

Patrick Van Newburg For school I need to make a react app, this is my first time working with html, css, javascript and react and now I am stuck at the part where I have to post data to the API. What I'm trying to do is create a page where people can choose to

How can I use my value-added in other components

Patrick Van Newburg For school I need to make a react app, this is my first time using html, css, javascript and react and now I am stuck at the part where I have to post data to the API. What I'm trying to do is create a page where people can choose to eat an