Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Constant Values to Angular from _layout.cshtml

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 that?

For example, here is one value I am trying to transfer.

var lenderValues = @Html.Action("GetLenderDropdownValues", "Dropdown");

and here is how my first app.component boots up.

<my-rite-ui>Loading...</my-rite-ui>

I know how to do it in AngularJS 1.x with just constant and injecting that constant whereever is needed, but couldn't figure it out in Angular.

Any help will be highly appreciated.

like image 798
premsh Avatar asked May 19 '16 00:05

premsh


2 Answers

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:

<script>
  var lenderValues = @Html.Action("GetLenderDropdownValues", "Dropdown");
  System.import('app/main').then((module) => {
    module.main(lenderValues);
  });
</script>

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.

like image 158
Thierry Templier Avatar answered Nov 18 '22 20:11

Thierry Templier


The first answer above was my favorite if you are using JIT compilation. We are using JIT for development and AOT for deployment. I couldn't find a good way to make the first answer work with AOT. For reference I followed the AOT cookbook here... Angular AOT cookbook.

The approach below will not work for server side rendering but should suffice for both JIT and AOT client side rendering.

1) In the razor view, _layout.cshtml for example, just put a script block and set a JSON object on the window interface. I placed this block within the "head" tag but doesn't really matter. The values for the JSON keys can be determined by any razor syntax.

<script type="text/javascript">
    window.appContext = {
        userName: '@("test".ToUpper())',
        isAdmin: @(1 == 1 ? "true" : "false")
    };
</script>

2) Create an app context service and wire up in appropriate module and inject into components for usage, if you need help with this just comment and I'll supply more info.

import { Injectable } from '@angular/core';

/*
    must match definition in SCRIPT tag that is seeded from razor
*/
interface IAppContext {
    userName: string;

    isAdmin: boolean;
}

/*
    
*/
@Injectable()
export class AppContextService implements IAppContext {

    userName: string;

    isAdmin: boolean;

    constructor() {
        var appContextBootstrap: IAppContext = (<IAppContext>(<any>window).appContext);

        this.userName = appContextBootstrap.userName;
        this.isAdmin = appContextBootstrap.isAdmin;
    }

}

3) reference and use the app context service throughout the application.

import { Component } from '@angular/core';

import { AppContextService } from './appContext.service'

@Component({
    moduleId: module.id,
    selector: 'story-app',
    templateUrl: 'app.component.html'
})
export class AppComponent {
    AppConfig: any;
    constructor(private appContext: AppContextService) { }

    ngOnInit() {

        alert('hi - ' + this.appContext.userName);
    }
}
like image 4
Jeff Marino Avatar answered Nov 18 '22 21:11

Jeff Marino