Javascript 获取 MVC 模型数据并将其传递给 AngularJS 控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30715492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:38:15  来源:igfitidea点击:

Getting and passing MVC Model data to AngularJS controller

javascriptasp.net-mvcangularjsrazorasp.net-mvc-5

提问by Daath

I'm pretty new to AngularJS and I'm at a loss here.

我对 AngularJS 还很陌生,我在这里不知所措。

Right now my MVC program uses Razor to display all the data in my .mdf database (i.e: @Html.DisplayFor(modelItem => item.LastName)). However, I want to go mostly Angular. I am trying to use ng-repeat to display all of the Model data, but I am not sure how to pass that Model data to the Angular controller and then use it. I have tried serializing the Model to JSON in my ng-init, but I don't think I'm doing it right (obviously).

现在我的 MVC 程序使用 Razor 来显示我的 .mdf 数据库中的所有数据(即:@Html.DisplayFor(modelItem => item.LastName))。但是,我想主要使用 Angular。我正在尝试使用 ng-repeat 来显示所有模型数据,但我不确定如何将该模型数据传递给 Angular 控制器然后使用它。我尝试在我的 ng-init 中将模型序列化为 JSON,但我认为我做得不对(显然)。

Here is my code:

这是我的代码:

// controller-test.js

var myApp = angular.module('myModule', []);

myApp.controller('myController', function ($scope) {
    $scope.init = function (firstname) {
        $scope.firstname = firstname;
    }
});
<!-- Index.cshtml -->

@model IEnumerable<Test.Models.Employee>

@{
    ViewBag.Title = "Index";
}

<div ng-app="myModule">
    <div ng-controller="myController" ng-init="init(@Newtonsoft.Json.JsonConvert.SerializeObject(Model))">
         <table>
             <tr ng-repeat= <!--THIS IS WHERE I'M STUCK -->
         </table>
    </div>
</div>

<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/controller-test.js"></script>
@Scripts.Render("~/Scripts/angular.js")

I'm not sure exactly what I should be repeating on to get the FirstName from the serialized Model. I feel like I have all the pieces, but just unsure how to connect them.

我不确定我应该重复什么才能从序列化模型中获取 FirstName。我觉得我有所有的部分,但只是不确定如何连接它们。

采纳答案by darkstalker_010

If you have the key firstNameon your Json object like:

如果您firstName的 Json 对象上有密钥,例如:

{
 "employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter","lastName":"Jones"}
  ]
}

You can do it in the following way.

您可以通过以下方式进行。

On your controller:

在您的控制器上:

myApp.controller('myController', function ($scope) {
    $scope.init = function (employees) {
        $scope.employees = employees;
    }
});

On your view:

在您看来:

<table>
    <tr ng-repeat= "employee in employees">
        <td>{{ employee.firstName }}<td>
    </tr>
</table>

回答by Daath

Thank you to darkstalker_010!

感谢darkstalker_010!

What I was confused was with how my Angular controller file interacted with the view. All I had to do was simply treat my angular {{ }} data in my .cshtml file as if I were trying to access the Model data normally (i.e. model.AttributeName)

我感到困惑的是我的 Angular 控制器文件如何与视图交互。我所要做的就是将 .cshtml 文件中的 angular {{ }} 数据视为尝试正常访问模型数据(即 model.AttributeName)

So here is the updated, working code:

所以这里是更新的工作代码:

// controller-test.js

var myApp = angular.module('myModule', []);

myApp.controller('myController', function ($scope) {
    $scope.init = function (employees) {
        $scope.employees= employees;
    }
});
<!-- Index.cshtml -->

@model IEnumerable<Test.Models.Employee>

@{
    ViewBag.Title = "Index";
}

<div ng-app="myModule">
<div ng-controller="myController" data-ng-init="init(@Newtonsoft.Json.JsonConvert.SerializeObject(Model))">
            <table>
                <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Title</th>
                    <th>Department</th>
                    <th>Email</th>
                </tr>
                <tr ng-repeat="e in employees">
                    <td>{{e.FirstName}}</td>
                    <td>{{e.LastName}}</td>
                    <td>{{e.Title}}</td>
                    <td>{{e.Department.DepartmentName}}</td>
                    <td>{{e.Email}}</td>
                </tr>
            </table>
</div>
</div>


<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/controller-test.js"></script>
@Scripts.Render("~/Scripts/angular.js")

Here is what it looks like sans formatting:

这是无格式的样子:

capture

捕获