如何将数据从服务器控制器发送到控制器 AngularJS?

我遇到了我认为我使用模板的常见方案 MVC /特别是框架 ASP.NET's MVC/ 适用于Web应用程序 AngularJS 在前端。 我的问题是,我有一定的价值,这是模型传输到我想到的模型的一部分,我也想让它可用 $scope, 我的控制器 Angular 理想情况下,一旦控制器初始化。

如何提出已被提出的问题和给出答案的问题。 有一个明显的候选人:
ngInit

. 但是在某些时候 Angular 更新
http://docs.angularjs.org/api/ng/directive/ngInit/
显然是针对这种特定思想的警告的事实:

单一适当用途
ngInit

- 这种平滑的特殊属性
ngRepeat

, 如下面的演示所示。 此外,在这种情况下,控制器应用于初始化该区域中的值,而不是
ngInit

.

建议的替代方案不是很重要。

当然,还有其他解决方法可以想出。 例如,视图可以在指令中插入值
ngModel

用于隐藏的输入。 或者我可以简单地忽略警告和使用
ngInit

反正。 但我所能提出的一切 - 这是一个更丑陋的方式来做同样的事情 , 我。
ngInit

, 要么明显更糟糕。

最终,在我看来明显的决定显然是不正确的,可能是我的思想与如何对应的指标 Angular 必须完成。 因此,我的问题不是 "如何应对这种情况", 事实:

像我这样的

交易

用这个脚本或避免它?

我为什么不使用
ngInit

?

解释,由于前两个评论,这不清楚:这是指某些或大多数页面直接为视图提供服务的情况 MVC, 但只提供了一些特定的功能 Angular. 我要传输到我的控制器的数据 Angular, 已经传输到模型中的演示文稿。 我不想要控制器 Angular 然后他去了,不得不做出自己的要求 get 仅对服务器获取相同的参数,该参数已以另一种格式呈现。
已邀请:

冰洋

赞同来自:

您必须将其从服务器控制器传递给控制器​​。 AngularJS 使用供应商 'value' 或者 'constant', 如此所述:
https://docs.angularjs.org/guide/providers
例如,您可以执行以下操作:


<script>
angular.module/"hobbitModule"/.value/"companionship", @Html.Raw/Model//;
</script>


然后将其输入到您的控制器中


var module = angular.module/"hobbitModule"/;
module.controller/"CompanionshipController", function/$scope, companionship/ {
$scope.companions = companionship;
}/;


如本文所述:
http://blog.mariusschulz.com/2 ... t-mvc
如果您认为它可能比只有一个值更复杂,您可以使用服务提供商并输入它而不是值提供程序。

知食

赞同来自:

假设你有这个模型 :

模型


public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public string Description { get; set; }
}


因此,您可以以自己的方式从控制器中传输数据 :

控制器


public string GetSerializedProduct//
{
var products = new[]
{
new Product{Id=1,Name="product1",Price=4500,Description="description of this product"},
new Product{Id=2,Name="product2",Price=500,Description="description of this product"},
new Product{Id=3,Name="product3",Price=400,Description="description of this product"},
new Product{Id=4,Name="product4",Price=5500,Description="description of this product"},
new Product{Id=5,Name="product5",Price=66500,Description="description of this product"}
};
var settings = new JsonSerializerSettings { ContractResolver=new CamelCasePropertyNamesContractResolver//};
return JsonConvert.SerializeObject/products,Formatting.None,settings/;
}
}


手表

:


@model string
<div class="container" ng-init="products = @Model">
<div class="row">
<div class="col-lg-12">
<table class="table table-condensed table-hover">
<tr>
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Description</th>
</tr>
<tr ng-repeat="product in products">
<td>{{product.id}}</td>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.description}}</td>
</tr>
</table>
</div>
</div>
</div>

要回复问题请先登录注册