我正在创建一个带有角度的web api。我的HTML代码出了问题。如果我只是调用web api,我会得到所需的数据,但是当我尝试在HTML中打印出来时,我得不到结果。请检查下面的代码。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Angular CRUD</title>
<link href="Content/bootstrap.css" rel="stylesheet"/>
<script src="Scripts/angular.js"></script>
<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/bootstrap.js"></script>
<script>
var app = angular.module('myApp', [])
app.controller("EmployeeCtrl", function ($scope, $http) {
getEmployees();
var getEmployees = function () {
alert("SDFS");
$http.get('/api/Employee')
.then(function (response) {
$scope.Employee = response.data
},
function () {
alert("Error in retrieving data");
})
}
})
</script>
</head>
<body ng-app="myApp" ng-contoller="EmployeeCtrl">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Employee ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Employee Code</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in Employee">
<td>{{item.EmployeeId}}</td>
<td>{{item.FirstName}}</td>
<td>{{item.LastName}}</td>
<td>{{item.EmployeeCode}}</td>
<td>{{item.Position}}</td>
<td>{{item.Office}}</td>
</tr>
</tbody>
</table>
</body>
</html>
您会注意到我甚至创建了一个警报,以检查是否调用了该函数,但显然未调用该函数。你能帮忙吗?谢谢。
答案 0 :(得分:2)
您的代码存在两个问题:
[&1]
。ng-controller
时,您无法在定义之前使用该函数。将其更改为var funcName = function () { ... }
解决这些问题,它会起作用!
答案 1 :(得分:0)
js代码逐行执行,在声明之前调用你的函数 尝试使用此代码而不是控制器:
<script>
var app = angular.module('myApp', [])
app.controller("EmployeeCtrl", function ($scope, $http) {
getEmployees();
var getEmployees=function() {
alert("SDFS");
$http.get('/api/Employee')
.then(function(response) {
$scope.Employee = response.data
},
function() {
alert("Error in retrieving data");
})
}
})
</script>