【发布时间】:2016-08-12 08:28:54
【问题描述】:
我有 3 个组件,web api、控制器和 html。我可以很好地访问 web api,然后以 JSON 格式返回结果,但是当它尝试将 JSON 呈现为 html 时,它看起来像这样。
{
"Projects": [{
"ProjectId": 1,
"Name": "Project1",
"Key": "Software",
"ProjectTypeKey": "1"
}, {
"ProjectId": 2,
"Name": "Project2",
"Key": "Hardware",
"ProjectTypeKey": "2"
}, {
"ProjectId": 3,
"Name": "Project3",
"Key": "Hardware",
"ProjectTypeKey": "3"
}]
}
WebAPI
public IHttpActionResult Get()
{
listProjects.Add(new Project { ProjectId = 1, Name = "Project1", Key = "Software", ProjectTypeKey = "1" });
listProjects.Add(new Project { ProjectId = 2, Name = "Project2", Key = "Hardware", ProjectTypeKey = "2" });
listEmployeeProject.Add(new EmployeeProject {Projects = listProjects });
return Json(listEmployeeProject);
}
控制器
var myApp = angular.module('myApp', []);
myApp.service('dataService', function ($http) {
this.getData = function () {
// $http() returns a $promise that we can add handlers with .then()
return $http({
method: 'GET',
url: '/api/employee'
});
}
});
myApp.controller('ProjectController', function ($scope, dataService) {
$scope.Projects = [];
dataService.getData().then(function (result) {
$scope.Projects = result.data;
});
});
HTML
<div ng-app="myApp" ng-controller="ProjectController">
{{1 + 1}}
<div ng-repeat="project in Projects">
{{project}}
</div>
即使我将 {{project}} 切换到 {{project.Name}},页面上也不会呈现任何内容。
console.log(results.data) 如下所示
【问题讨论】:
-
{{1 + 1}}被渲染了吗? -
是的,这呈现为 2
-
必须检查您在
result.data中得到了什么? -
console有任何错误吗?在您的thenfunction中添加console.log(result.data)。 -
results.data 是一个包含 3 个对象的数组
标签: angularjs json rest asp.net-web-api