简体   繁体   English

Angular2如何从MVC传递应用程序配置?

[英]Angular2 how to pass application config from MVC?

Trying to upgrade from RC4 => Angular 2.1.0 尝试从RC4升级=> Angular 2.1.0

 var native = this.elementRef.nativeElement;
 var myattr = native.getAttribute("applicationConfig");

I am getting: 我正进入(状态:

Exception: Call to Node module failed with error: TypeError: native.getAttribute is not a function 例外:调用节点模块失败并显示错误:TypeError:native.getAttribute不是函数

My objective is to ass applicationConfig from MVC side to angular. 我的目标是将来自MVC端的applicationConfig应用于角度。 It used to work that we would set a global object and access it through window['objName'] on the angular side. 以前我们设置一个全局对象并通过角度侧的窗口['objName']访问它。 How do I pass my config to angular now? 如何将配置传递给角度? I have tried elementRef trick but it seems like it's not working at all in 2.1.0 我已经尝试过elementRef技巧,但似乎它在2.1.0中根本不起作用

<app asp-prerender-module="ClientApp/boot-server"
         asp-prerender-webpack-config="webpack.config.js"
         applicationConfig="@ViewBag.appConfig">Loading...</app>

I am using the element ref in my bootstrapped component - AppComponent 我在我的bootstrapped组件中使用了元素ref - AppComponent

When my ng2 app starts, I need to pass a few server-side variables to the app. 当我的ng2应用程序启动时,我需要将一些服务器端变量传递给应用程序。 Here is what my razor view looks like: 这是我的剃刀视图:

 @ModelType MyProject.Models.MasterViewModel <!DOCTYPE html> <html> <head> <base href="/"> <script type="text/javascript" src="~/assets/js/inline.bundle.min.js"></script> </head> <body> <app>Loading...</app> <script> var APP_SETTINGS = { apiUrl: "@Model.ApiUrl", ipAddress: "@Model.IpAddress", referredFrom: "@Model.ReferredFrom", browser: "@Model.Browser" } </script> <script type="text/javascript" src="~/assets/js/main.bundle.min.js"></script> </body> </html> 

Then when I want to reference APP_SETTINGS, I do something like the following (within the context of my ng2 app): 然后,当我想引用APP_SETTINGS时,我会执行以下操作(在我的ng2应用程序的上下文中):

 export interface IAppSettings { apiUrl: string; ipAddress: string; referredFrom: string; browser: string; } declare const APP_SETTINGS: IAppSettings; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM