Pass Constant Values to Angular from _layout.cshtml

前端 未结 3 476
无人及你
无人及你 2021-01-05 17:52

Okay, so I have a constant variables in the _Layout.cshtml of ASP.Net SPA project and I would to pass them so that Angular will have access to those.

How can I do t

3条回答
  •  自闭症患者
    2021-01-05 18:39

    It depends on what you want exactly to do but I see two possibilities:

    • Define a constant

    This constant needs to be passed when importing your main module. Whereas it's not possible to do this on the import itself. You can import a function and call it with parameters.

    Here is a sample of the main module that bootstraps your application:

    import {bootstrap} from '...';
    import {provide} from '...';
    import {AppComponent} from '...';
    
    export function main(lenderValues) {
      bootstrap(AppComponent, [
        provide('lenderValues', { useValue: lenderValues })
      ]);
    }
    

    Then you can import it from your HTML main page like this:

    
    

    Your lenderValues can be then injected in all elements like a component:

    @Component({
      (...)
    })
    export class SomeComponent {
      constructor(@Inject('lenderValues') lenderValues) {
      }
    }
    
    • Define a parameter on my-rite-ui element

    You can specify a parameter at this level but it can't be evaluated. So it's a bit more tedious since you need to serialize it as string with JSON.stringify, gets the element in your component from its corresponding ElementRef and deserialize it using JSON.parse.

    Then you can add the data as a provider.

提交回复
热议问题