从_layout.cshtml通常量值2角 [英] Pass Constant Values to Angular 2 from _layout.cshtml

查看:152
本文介绍了从_layout.cshtml通常量值2角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我在ASP.Net SPA项目_Layout.cshtml常数变量,我会通过他们,使角2将有机会获得的。

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 2 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");

这是我的第一个app.component靴子怎么涨。

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

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

我知道如何只用恒定的,注射是需要不断的徘徊无论做,在Angular1.x,但无法弄清楚在角2

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

任何帮助将是非常美联社preciated。

Any help will be highly appreciated.

推荐答案

这取决于你想究竟是什么做的,但我看到两种可能:

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 })
  ]);
}

然后你可以从你的HTML主页导入它是这样的:

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>

lenderValues​​ 可以像一个组件中的所有元素然后注入:

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

@Component({
  (...)
})
export class SomeComponent {
  constructor(@Inject('lenderValues') lenderValues) {
  }
}


  • 定义参数我爱色丽的UI 元素

  • 可以指定在这个级别的参数,但是它不能被评价。所以这是一个有点比较繁琐的,因为你需要序列作为字符串 JSON.stringify ,获取元素从它对应的 ElementRef 并使用反序列化 JSON.parse

    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.

    这篇关于从_layout.cshtml通常量值2角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆