我无法解决我的问题,请查看我的代码并为其提供解决方案
这是server.js的代码
var express = require('express');
var app = express();
app.use(express.static(__dirname + "/public"))
app.listen(3000);
console.log("server running on port 3000")
这是controller.js的代码
var app = angular.module('myApp', []);
app.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
$http({
method: 'GET',
url: '/employeelist'
}).then(function (response) {
> console.log("I got the data I requested");
$scope.employeelist = response.data;
});
employee1 = {
name: 'Sunil',
designation: 'Software Developer',
salary: '20000'
};
employee2 = {
name: 'Vamshi',
designation: 'Java Developer',
salary: '30000'
};
employee3 = {
name: 'Chethan',
designation: 'Dot Net Developer',
salary: '20000'
};
var employeeList = [employee1, employee2, employee3];
$scope.employeelist = employeelist;
}]);
这是html文件的代码
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">
<title>Employees List</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>Employees List</h1>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Designation</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employeelist">
<td>{{employee.name}}</td>
<td>{{employee.designation}}</td>
<td>{{employee.salary}}</td>
</tr>
</tbody>
</table>
</div>
<script src ="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/
angular.min.js"></script>
<script src="controllers/controller.js"></script>
</body>
</html>
错误是关于angular的版本还是我的代码中有什么问题?
节点服务器工作正常,但是当我打开localhost时,上面的错误正在显示,我也无法查看此行 console.log(“我收到了我要求的数据”); < / p>
答案 0 :(得分:3)
这是因为 JavaScript区分大小写
您的本地变量名为employeeList
,但您尝试将employeelist
分配给$scope.employeelist
,employeelist is not defined
。
改变这个:
var employeeList = [employee1, employee2, employee3];
$scope.employeelist = employeelist;
对此:
var employeeList = [employee1, employee2, employee3];
$scope.employeelist = employeeList; // uppercase "L"
答案 1 :(得分:0)
Javascript区分大小写,您var employeeList
后跟$scope.employeelist = employeelist;
将第一个更改为var employeelist
,它应该有效