Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何从mvc传递到angular 2并键入脚本?_Asp.net Mvc_Angular_Typescript - Fatal编程技术网

Asp.net mvc 如何从mvc传递到angular 2并键入脚本?

Asp.net mvc 如何从mvc传递到angular 2并键入脚本?,asp.net-mvc,angular,typescript,Asp.net Mvc,Angular,Typescript,我需要一些帮助。我是新手。我想要的是将一些数据(字符串变量'element')传递到我的UI,然后我想要使用它。我不知道如何将它传递到main.js内部以及更进一步。如何将变量从mvc服务器传递到typescript文件? Index.cshtml: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <app-root>Loading...</app-root> 和我的app

我需要一些帮助。我是新手。我想要的是将一些数据(字符串变量'element')传递到我的UI,然后我想要使用它。我不知道如何将它传递到main.js内部以及更进一步。如何将变量从mvc服务器传递到typescript文件? Index.cshtml:

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<app-root>Loading...</app-root>
和我的app.ts文件:

 import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { ParamsComponent } from './app/ccg-disk-cam-params/disk-cam-
 params.component';
import { CenterComponent } from './app/ccg-center/ccg-center.component';
import { ResultsComponent } from './app/ccg-results/ccg-results.component';
import { OutputComponent } from './app/ccg-output/ccg-output.component';
import { CylinderTranslatingComponent } from './app/ccg-disk-cam-follower-
params/cylinder-translating.component';
import { CylinderSwingingArmComponent } from './app/ccg-disk-cam-follower-
params/cylinder-swinging-arm.component';
import { SphereTranslatingComponent } from './app/ccg-disk-cam-follower-
params/sphere-translating.component';
import { SphereSwingingArmComponent } from './app/ccg-disk-cam-follower-
params/sphere-swinging-arm.component';
import { ModalComponent } from './app/ccg-modal/ccg-modal.component';
import { HttpModule } from '@angular/http';


@NgModule({
 imports: [BrowserModule, HttpModule, FormsModule ],
 declarations:
 [
    AppComponent,
    ParamsComponent,
    CenterComponent,
    ResultsComponent,
    OutputComponent,
    CylinderTranslatingComponent,
    CylinderSwingingArmComponent,
    SphereSwingingArmComponent,
    SphereTranslatingComponent,
    ModalComponent
],
 bootstrap: [AppComponent]
})
export class AppModule { }

您可以创建全局javascript变量,也可以使用本地存储

全局变量:

升压前:

<script>
        var element = "@elementServer";    
        var globalVariable = element;            
</script>
使用本地存储:

这是我以前使用过的一种解决方法。您可以将razor变量传递给javascript,然后将其保存在localStorage中。然后,您可以从localStorage检索该值

对于敏感数据,这两种方法均不推荐使用

因此,在引导angular2之前:

<script>
    var element = "@elementServer";    
    localStorage.setItem('elementServer',element);
</script>

您可以创建全局javascript变量,也可以使用本地存储

全局变量:

升压前:

<script>
        var element = "@elementServer";    
        var globalVariable = element;            
</script>
使用本地存储:

这是我以前使用过的一种解决方法。您可以将razor变量传递给javascript,然后将其保存在localStorage中。然后,您可以从localStorage检索该值

对于敏感数据,这两种方法均不推荐使用

因此,在引导angular2之前:

<script>
    var element = "@elementServer";    
    localStorage.setItem('elementServer',element);
</script>

您想在角度侧的何处使用它?最佳方案-在应用程序目录中的任何ccg-..…ts文件中。在MVC应用程序上公开Get方法,并从角度向其发出Http请求。您想在角度侧的何处使用它?最佳方案-在应用程序目录中的任何ccg-..…ts文件中。在您的应用程序上公开Get方法MVC应用程序,并向其发出Http请求。
<script>
    var element = "@elementServer";    
    localStorage.setItem('elementServer',element);
</script>
var lstorage = localStorage.getItem("elementServer");