从MVC模型设置角度模型 [英] Set angular model from MVC model

查看:60
本文介绍了从MVC模型设置角度模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循此指南: http://blog.mariusschulz.com/2014/03/25/bootstrapping-angularjs-applications-with-server-side-data-from-aspnet-mvc

这段代码对我来说很好用,即我可以看到@ Html.Raw(Model)的输出,并且它是有效的JSON

This code works fine for me, i.e. I can see the output of @Html.Raw(Model) and it is valid JSON

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

但是,当我运行这段代码时

However when I run this code

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

同伴关系"变量始终未定义.这是我的版本

The "companionship" variable is always undefined. Here is my version

<script type="text/javascript">      
angular.module("myApp.controllers").value("companionship",@Html.Raw(JsonConvert.SerializeObject(Model)));
</script>

ng.module('myApp.controllers')
    .controller('AccountCtrl', ['$scope', function ($scope, companionship) {
        $scope.companions = companionship;
}]);

推荐答案

您应该在$ scope旁边的控制器中添加"companionship"注释

You should be adding 'companionship' annotation next to $scope into your controller

ng.module('myApp.controllers')
.controller('AccountCtrl', ['$scope', 'companionship', function ($scope, companionship) {
    $scope.companions = companionship;
}]);

这篇关于从MVC模型设置角度模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆