简体   繁体   English

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

[英]Getting and passing MVC Model data to AngularJS controller

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 (ie: @Html.DisplayFor(modelItem => item.LastName) ). 现在我的MVC程序使用Razor显示我的.mdf数据库中的所有数据(即: @ Html.DisplayFor(modelItem => item.LastName) )。 However, I want to go mostly Angular. 但是,我想主要是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. 我正在尝试使用ng-repeat来显示所有Model数据,但我不确定如何将该Model数据传递给Angular控制器然后使用它。 I have tried serializing the Model to JSON in my ng-init, but I don't think I'm doing it right (obviously). 我已经尝试在我的ng-init中将Model序列化为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. 我不确定我应该重复从序列化模型中获取FirstName。 I feel like I have all the pieces, but just unsure how to connect them. 我觉得我有所有的部分,但只是不确定如何连接它们。

If you have the key firstName on your Json object like: 如果你的Json对象上有key firstName ,比如:

{
 "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>

Thank you to darkstalker_010! 谢谢darkstalker_010!

What I was confused was with how my Angular controller file interacted with the view. 我感到困惑的是我的Angular控制器文件如何与视图交互。 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 (ie model.AttributeName) 我所要做的只是简单地处理我的.cshtml文件中的角度{{}}数据,好像我正在尝试正常访问模型数据(即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: 这是sans格式化的样子:

捕获

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

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