gpt4 book ai didi

javascript - AngularJs 不渲染值

转载 作者:行者123 更新时间:2023-11-28 13:09:29 27 4
gpt4 key购买 nike

我是 AngularJS 新手,需要使用 AngularJs 渲染我的 MVC Controller Json 输出。下面是我输出 Json 的 MVC Controller :

        [HttpGet]
public JsonResult GetAllData()
{
int Count = 50;
return Json(Workflow.Teacher.GetTeachers(Count), JsonRequestBehavior.AllowGet);
}

我的 Angular Controller 调用 GetAllData Action 方法:

    angular.module('myFormApp', [])
.controller('HomeController', function ($scope, $http, $location, $window) {
$scope.teacherModel = {};
$scope.message = '';
$scope.result = "color-default";
$scope.isViewLoading = false;
$scope.ListTeachers = null;
getallData();

//******=========Get All Teachers=========******
function getallData() {
$http({
method: 'GET',
url: '/Home/GetAllData'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
$scope.ListTeachers = response;
console.log($scope.ListTeachers);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
$scope.errors = [];
$scope.message = 'Unexpected Error while saving data!!';
console.log($scope.message);
});
};

})
.config(function ($locationProvider) {
$locationProvider.html5Mode(true);
});

此外,我的 MVC 布局标记如下:

@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Teachers List</h2>

<div id="content" ng-controller="HomeController">
<span ng-show="isViewLoading" class="viewLoading">
<img src="~/Content/images/ng-loader.gif" /> loading...
</span>
<div ng-class="result">{{message}}</div>

<table class="table table-striped">
<tr ng-repeat="teacherModel in ListTeachers">
<td>{{teacherModel.TeacherNo}}</td>
<td>{{teacherModel.TeaFName}}</td>
<td>{{teacherModel.TeaSName}}</td>
</tr>
</table>

</div>

@section JavaScript{
<script src="~/Scripts/angular.js"></script>
<script src="~/ScriptsNg/HomeController.js"></script>
}

除了上面我的主布局的 body 标签之外,还有 ng-app

<body ng-app="myFormApp" >

我正在使用 MVC 版本 5 和 AngularJs v1.6.4。在调试时,我可以确认它确实调用了 getallData() actionMethod 并且确实以 Json 形式返回行。我没有收到任何错误,但它也没有渲染模型值。

如有任何建议,我们将不胜感激。

最佳答案

使用response.data来捕获数据。

改变

$scope.ListTeachers = response;

到此

$scope.ListTeachers = response.data;

关于javascript - AngularJs 不渲染值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43888327/

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