Sisky
Sisky

Reputation: 603

Using static variable across components

I have a data-store TypeScript file which has a few String arrays:

export const servicesData =
    [
        'test',
        'test',
    ];

export const surgeonsData =
    [
        'test',
        'test',
    ];

I then have another Component of which I have multiple instantiated at a time. I want to use the data-store arrays in a static way so I can use them across all instances without reading each time.

Here is a quick abstract of what I have now. Every booking component needs to use the array from the data-model.ts file without rereading it each time.

import { surgeonsData, servicesData } from 'app/data-models.ts'

@Component({
    selector: 'booking',
    template:
        `
          <div *ngFor="let s of surgeons">
            {{s}}
          </div>
        `
})


export default class BookingComponent {

    surgeons = surgeonsData;
    services = servicesData;

    constructor() {}


}

So in my above example every time a new component is made the surgeonData is saved in to the surgeon variable. I want to instead of doing this every time have only one variable that every component can access.

Upvotes: 0

Views: 7531

Answers (1)

wilsonhobbs
wilsonhobbs

Reputation: 951

Use a service to have access to globally-available, static variables. Trying to do this outside of services will run into problems with Zoning and change detection.

Upvotes: 1

Related Questions