Javascript 角度未加载API-第一个应用程序
我开始在AngularJS的世界 我已经创建了一个小应用程序,它应该从带有一些虚拟值的API中提取数据。API工作并提供以下JSON(键入/API/employees/): 为了使用此功能,我创建了以下内容:Javascript 角度未加载API-第一个应用程序,javascript,angularjs,Javascript,Angularjs,我开始在AngularJS的世界 我已经创建了一个小应用程序,它应该从带有一些虚拟值的API中提取数据。API工作并提供以下JSON(键入/API/employees/): 为了使用此功能,我创建了以下内容: (function () { 'use strict'; config.$inject = ['$routeProvider', '$locationProvider']; angular.module('empApp', [ 'ngRoute', 'employeesServices
(function () {
'use strict';
config.$inject = ['$routeProvider', '$locationProvider'];
angular.module('empApp', [
'ngRoute', 'employeesServices'
]).config(config);
function config($routeProvider, $locationProvider) {
$routeProvider
.when('/test', {
templateUrl: '/Views/test.html',
controller: 'TestCtrl'
})
.when('/', {
templateUrl: '/Views/list.html',
cntroller:'EmployeeListController'
})
.otherwise({
redirectTo: '/'
});
//$locationProvider.html5Mode(true);
}
angular.module('empApp')
.controller('TestCtrl', TestCtrl);
// Test Contoller - ngView
TestCtrl.$inject = ['$scope'];
function TestCtrl($scope) {
$scope.model = {
message: "This is my app!!!"
};
};
// Employee List Controller
EmployeeListController.$inject = ['$scope', 'Employee'];
function EmployeeListController($scope, Employee) {
$scope.employees = Employee.query();
};
// Employees Service
angular
.module('employeesServices', ['ngResource'])
.factory('Employee', Employee);
Employee.$inject = ['$resource'];
function Employee($resource) {
return $resource('/api/employees/:id');
};})();
目前我还没有将代码分离到单独的文件中,但我确实打算这样做。此外,请提供任何建议,重新命名公约等任何帮助是感激的
我有以下部分html文件:
<div>
<h1>List Employees</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th></th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="emp in employees">
<td>
<a href="/employees/edit/{{emp.Id}}" class="btn btn-default btn-xs">edit</a>
<a href="/employees/delete/{{emp.Id}}" class="btn btn-danger btn-xs">delete</a>
</td>
<td>{{emp.FirstName}}</td>
<td>{{emp.LastName}}</td>
</tr>
</tbody>
</table>
<p>
<a href="/employees/add" class="btn btn-primary">Add New Employee</a>
</p></div>
列出雇员名单
名字
姓
{{emp.FirstName}
{{emp.LastName}
index.html文件包含:
<!DOCTYPE html>
<html ng-app="empApp">
<head>
<meta charset="utf-8" />
<title></title>
<script src="lib/angular/angular.js"></script>
<script src="lib/angular-resource/angular-resource.js"></script>
<script src="lib/angular-route/angular-route.js"></script>
<script src="app.js"></script>
</head>
<body>
<h1>Hello Angular JS</h1>
What is 2+2 ?
<br /><br />
Answer = {{2+2}}
<br /><br />
<ng-view></ng-view>
</body>
</html>
你好,J
什么是2+2?
答案={{2+2}}
Angular在表达式确认为4时起作用。但是我没有从API中获得任何数据。
请帮忙 我注意到您在路由定义中有一个输入错误(
cntroller:'EmployeeListController'
)。
(而且我没有足够的声誉发表评论)没错。可能您的EmployeeListController从未被错误的声明“cntroller”实例化过,并且您的employees变量具有未定义的值。经过大量阅读和网页后,我意识到我没有将控制器定义为可供模块使用,因此我的代码应为:
angular.module('empApp')
.controller('TestCtrl', TestCtrl)
.controller('EmployeeListController', EmployeeListController);
谢谢你的关注 非常感谢您注意到输入错误,但是nope nothing更改仍然没有数据非常感谢您注意到输入错误,但是nope nothing更改仍然没有数据使用开发人员工具时出现以下错误:错误:[ng:areq]参数“EmployeeListController”不是函数,未定义
angular.module('empApp')
.controller('TestCtrl', TestCtrl)
.controller('EmployeeListController', EmployeeListController);