将asp.net服务器参数传递给Angular 2应用程序

vid*_*oon 6 asp.net-mvc asp.net-web-api angular

================================================== ==============

编辑:解决方案升级到2.0 Final后 - 在RC5升级后将服务器参数传递给ngModule

================================================== ================

有没有将服务器参数传递给Angular 2应用程序的方法?

即我想使用MVC对象"HttpContext.User.Identity.Name",并在我的角度2应用程序中的任何地方注入它.

在角度1中,这可以使用ng".constant"并将.Net对象序列化为index.cshtml中的JSON.

看起来有一种方法可以传递params,但这不适用于.Net代码. 在Angular 2中定义全局常量

        //HTML - Bootstrapping
        <script>

            System.import('app/main').then(null, console.error.bind(console));
            //I WOULD LIKE TO PASS SOME PARAMS TO APP/MAIN HERE
        </script>
Run Code Online (Sandbox Code Playgroud)

最终解决方案:(非常感谢蒂埃里)

index.cshtml:

<script>
    System.import('app/main').then(
        module => 
            module.main(
                {
                    name: '@User.Identity.Name',
                    isAuthenticated: User.Identity.IsAuthenticated.ToString().ToLowerInvariant(),
                }
            ),
        console.error.bind(console)
    );
</script>
Run Code Online (Sandbox Code Playgroud)

main.ts:

...
    import {provide} from '@angular/core';
...

    export function main(params) {
        bootstrap(AppComponent,
            [
                provide('Name', { useValue: params.name }),
                provide('IsAuthenticated', { useValue: params.isAuthenticated }),
                ROUTER_PROVIDERS,
                HTTP_PROVIDERS,
                LoggerService,
                AuthenticationService
            ]);
    }
Run Code Online (Sandbox Code Playgroud)

用法:

import {Component, Injectable, Inject} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';

@Component({
    selector: 'navbar',
    templateUrl: 'app/components/header/navbar.html',
    directives: [ROUTER_DIRECTIVES]
})
export class SomeComponent {

    constructor(@Inject('Name') public username: string) {

    }
Run Code Online (Sandbox Code Playgroud)

}

Thi*_*ier 9

一个选项是在您导入的模块中添加方法.因此,您可以调用它来提供您想要的对象.

以下是该app/main模块的示例:

import {bootstrap} from '...';
import {provide} from '...';
import {AppComponent} from '...';

export function main(params) {
  let userIdentityName = params.name; // for example
  bootstrap(AppComponent, [
    provide('userIdentityName', { useValue: userIdentityName })
  ]);
}
Run Code Online (Sandbox Code Playgroud)

然后你可以从HTML主页面导入它,如下所示:

<script>
  System.import('app/main').then((module) => {
    module.main({
      userIdentityName: 'something from asp.net'
    });
  });
</script>
Run Code Online (Sandbox Code Playgroud)

更新

使用最新版本的Angular,您需要以这种方式利用模块:

export const USER_IDENTITY_NAME_TOKEN =
  new  InjectionToken('userIdentityName');

@NgModule({
  (...)
  providers: [
    {
      provide: USER_IDENTITY_NAME_TOKEN,
      useValue: userIdentityName
    }
  ]
})
export class MainModule() { }
Run Code Online (Sandbox Code Playgroud)

  • 这在@Angular RC6中不起作用.@ angular/core不再支持"provide".任何解决方法请 (4认同)
  • 任何想法如何在RC5之后做到这一点?ngModule给了我很多问题. (3认同)