How do I store a local variable or cache it so that the results of an API call can be shared throughout the application?
I have the following Angular 6 module which basically creates a mentions directive to be able to do @mentions popular in many apps:
@NgModule({
    imports: [
        CommonModule
    ],
    exports: [
        MentionDirective,
        MentionListComponent
    ],
    entryComponents: [
        MentionListComponent
    ],
    declarations: [
        MentionDirective,
        MentionListComponent
    ]
})
export class MentionModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: MentionModule
        };
    }
}
This is the actual directive:
export class MentionDirective implements OnInit, OnChanges {
items: any[];
constructor(
  public autocompletePhrasesService: AutocompletePhrasesService
) { }
  ngOnInit() {
    this.autocompletePhrasesService.getAll()
      .pipe(first())
      .subscribe(
        result => {
          this.items = result;
        },
        () => { });
The directive calls a function in my core module which retrieves the data:
export class AutocompletePhrasesService {
    public autoCompletePhrases: string[];
    constructor(private http: HttpClient) { }
    getAll(): Observable<string[]> {
        return this.http.get<string[]>(this.baseUrl + '/getall');
    }
My problem is that this directive may have 20-30 instances on a single page and the API gets called for every instance of the directive. How can I change my code so that the API gets called just once per application instance?
The data does not change often from the API.
I have tried storing the results of the service in the public autoCompletePhrases variable, and only calling that if it is empty, but that hasn't worked
 
     
    