'How to access const(without export const variable) variable in my app.module and access it from children components in angular?

app.module.ts const test: boolean = false; // i want to access this variable to another component. //note: there is no export variable of test.

test.component.ts file here i want to access the const test varaible inside test component.

Please let me know how to access



Solution 1:[1]

You can use providers, but I'm not pretty sure if it is what are you looking for

//your module:

const test:boolean=false
@NgModule({
  imports:      [... ],
  declarations: [ ... ],
  providers: [{ provide: "TEST", useValue: test }],
  bootstrap:    [ AppComponent ]
})

//your component
constructor(@Inject("TEST") private config: string) {
    this.name = config?"test is true":"test is false";
}

(*)Usually you use uppercase to use value as provider, but you can use any string

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1