gpt4 book ai didi

javascript - 将 ASP.NET MVC 模型分配给 AngularJS 范围

转载 作者:数据小太阳 更新时间:2023-10-29 05:18:55 24 4
gpt4 key购买 nike

在我的 View 代码下面(javascript代码在 View 中,只是用于测试的临时代码)。

我想将 ASP.NET MVC 模型 (@Model) 分配给 AngularJS 作用域 ($scope.person)

我该怎么做?

谢谢,

景色

@model MyApp.Person

<script>
var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
$scope.person = ?????
}]);
</script>

更新 1:我在 JS 文件中尝试了这段代码:

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

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
$scope.person = @Html.Raw(window.person);
}]);

在 View 文件中:

<script>
@{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
}
window.person = serializer.Serialize(Model);
</script>

我得到 2 个错误:

ReferenceError: serializer is not defined (on windows)
window.person = serializer.Serialize(Model);

SyntaxError: illegal character (it's the @)
$scope.person = @Html.Raw(window.person);

最佳答案

<script>
@{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = serializer.Serialize(Model);
}
var myApp = angular.module('myApp', []);

myApp.controller('personController', ['$scope', '$http', function ($scope, $http) {
$scope.person = @Html.Raw(json);
}]);
</script>

关于javascript - 将 ASP.NET MVC 模型分配给 AngularJS 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26032205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com