[英]passing data from mvc controller to angular controller and scope
Within a MVC controller I am trying to pass a collection of data back to the view. 在MVC控制器中,我试图将数据集合传递回视图。
private string GetEntityList()
{
var entities = new[]
{
new EntityList{ EntityId = 1, EntityName = "Entity 1"},
new EntityList{ EntityId = 2, EntityName = "Entity 2"},
new EntityList{ EntityId = 3, EntityName = "Entity 3"},
new EntityList{ EntityId = 4, EntityName = "Entity 4"},
new EntityList{ EntityId = 5, EntityName = "Entity 5"}
};
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
return JsonConvert.SerializeObject(entities, Formatting.None, settings);
}
And I send it to the view via a string model 然后通过字符串模型将其发送到视图
public ViewResult Index()
{
return View("index","", GetEntityList());
}
Within my cshtml file I am trying to assign the model to a property called mvcData that a part of a service I define 在我的cshtml文件中,我试图将模型分配给名为mvcData的属性,该属性是我定义的服务的一部分
app.factory("searchAttributes", function() {
console.log(@Html.Raw(Model));
return { mvcData:@Html.Raw(Model) };
});
</script>
This script tag resides inside of my ng-app so its in the scope of the app however when I try to reference if from app.controller
it appears as undefined 这个脚本标签位于我的ng-app内部,因此它位于应用程序的范围内,但是当我尝试从
app.controller
引用时,它似乎未定义
app.controller('searchController', ['$scope', 'searchService', 'ngNotifier', '$log', '$timeout', 'searchAttributes', function ($scope, searchService, searchAttributes, ngNotifier, $log, $timeout) {
var vm = this;
//bootstraped data from MVC
$scope.searchAttributes = searchAttributes.mvcData;
}]);
Looking at the model when it is returned to the html I can see that it is a valid object. 查看返回到html的模型,我可以看到它是一个有效的对象。 Using
console.log(@Html.Raw(Model)
I see objects coming back 使用
console.log(@Html.Raw(Model)
我看到对象回来了
Object[0] entityId: 1 entityName: "Entity 1"
Object[1] entityId: 2 entityName: "Entity 2"
Any ideas why this object isn't being picked up in the controller? 有什么想法为什么不在控制器中拾取此对象? Even though I have it defined as a dependency it's as if the
ng-controller
doesn't detect it/load it. 即使我已将其定义为依赖项,也就好像
ng-controller
不会检测到/加载它。
thanks in advacnce 谢谢你
Your dependencies are out of order: 您的依存关系不正常:
app.controller('searchController', ['$scope', 'searchService', 'ngNotifier', '$log', '$timeout', 'searchAttributes', function ($scope, searchService, searchAttributes, ngNotifier, $log, $timeout) {
should be 应该
app.controller('searchController', ['$scope', 'searchService', 'ngNotifier', '$log', '$timeout', 'searchAttributes', function ($scope, searchService, ngNotifier, $log, $timeout, searchAttributes) {
Your current code would mean your searchAttributes.mvcData;
您当前的代码将表示您的
searchAttributes.mvcData;
is actually referencing timeout.mvcData
which is undefined. 实际上是引用
timeout.mvcData
,它是未定义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.