【发布时间】:2015-11-20 21:43:03
【问题描述】:
目前我收到上述错误。我认为我收到此错误很奇怪,因为我不想使用 Angular 的“路由”,而只是使用 Angular 来发布/获取数据。
我的 Angular 函数被拆分为一个模块、服务和控制器文件。 最重要的是,我使用了名为“EJS”的引擎。所以这意味着我在下面列出的 HTML 看起来有点不完整。这是因为我在“布局”文件中包含了大部分 HTML,其中加载了导航栏等基本内容。
HTML:
<% include ../layout %>
<body ng-app="clientModule">
<div class="container" data-ng-cloak data-ng-app="clientModule" data-ng-controller="clientController">
<form class="navbar-form navbar-left" role="search" method="POST" name="formClient">
<div class="row">
<div class="form-group">
<label for="">Client name</label>
<input type="text" class="form-control" placeholder="Please enter client name" name="clientName" ng-model="client.clientName" style="width: 100%" required>
</div>
</div>
<div> </div>
<div class="row">
<div class="form-group">
<label for="">Client address</label>
<input type="text" class="form-control" placeholder="Please enter client address" name="clientName" ng-model="client.clientAddress" style="width: 100%" required>
</div>
</div>
<div> </div>
<div class="row">
<div class="form-group">
<button type="button" class="btn btn-primary" ng-lick="createClient(client)">Create client</button>
</div>
</div>
</form>
</div>
</body>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-route/angular-route.js"></script>
<script src="../../controllers/clients/clientModule.js"></script>
<script src="../../controllers/clients/clientService.js"></script>
<script src="../../controllers/clients/clientController.js"></script>
模块:
var clientModule = angular.module('clientModule', []);服务:
angular.module('clientModule').controller('clientService', clientService); clientService.$inject = ['$http']; 功能客户端服务($范围){ 返回 { createClient : 函数(客户端){ return $http.post('/createClient', { 客户端名称:客户端.客户端名称, 客户端地址:客户端.客户端地址 } ); } }; }控制器:
angular.module('clientModule').controller('clientController', clientController); clientController.$inject = ['$scope', '$timeout', 'clientService']; 功能客户端控制器($范围,$超时,客户端服务){ $scope.client = { 客户名称 : ””, 客户地址:“” }; $scope.client = 功能(客户端){ clientService.createClient(client).success(function (data) { /* $超时(函数(){ alert("数据发布成功"); },3000) */ }); } }NodeJS 输出:
$节点服务器 服务器运行在端口:1337 GET /createClient 304 18.255 毫秒 - - 获取 /stylesheets/bootstrap.min.css 304 6.576 毫秒 - - 获取 /bower_components/bootstrap/dist/js/bootstrap.js 304 9.184 毫秒 - - 获取 /bower_components/jquery/dist/jquery.min.js 304 10.352 毫秒 - - 获取 /controllers/clients/clientService.js 304 4.351 毫秒 - - 获取 /bower_components/angular/angular.js 304 6.103 毫秒 - - 获取 /controllers/clients/clientModule.js 304 2.677 毫秒 - - 获取 /bower_components/angular-route/angular-route.js 304 3.597 毫秒 - - 获取 /controllers/clients/clientController.js 304 1.758 毫秒 - - 获取 /bower_components/jquery/dist/jquery.min.map 304 1.767 毫秒 - - 获取 /bower_components/angular-route/angular-route.js 304 0.733 毫秒 - - 获取 /controllers/clients/clientModule.js 304 0.755 毫秒 - - 获取 /controllers/clients/clientService.js 304 0.939 毫秒 - - 获取 /controllers/clients/clientController.js 304 1.101 毫秒 - -jQuery 路由文件:
功能客户端路由配置(应用程序){ this.app = 应用程序; this.routeTable = []; this.init(); } clientRouteConfig.prototype.init = function() { 变种自我=这个; this.addRoutes(); this.processRoutes(); } clientRouteConfig.prototype.processRoutes = function() { 变种自我=这个; self.routeTable.forEach(功能(路线){ if(route.requestType == 'get') { self.app.get(route.requestUrl, route.callbackFunction); } 否则 if(route.requestType == 'post') { } 否则 if(route.requestType == 'delete') { } }); } clientRouteConfig.prototype.addRoutes = function() { 变种自我=这个; self.routeTable.push({ requestType : 'post', requestUrl : '/createClient', 回调函数:函数(请求,响应){ response.render('../views/clients/createClient', { title: "创建客户端"}); } }); 变种自我=这个; self.routeTable.push({ 请求类型:'获取', requestUrl : '/createClient', 回调函数:函数(请求,响应){ response.render('../views/clients/createClient', { title: "创建客户端"}); } }); self.routeTable.push({ 请求类型:'获取', requestUrl : '/客户', 回调函数:函数(请求,响应){ response.render('../views/clients/clients', { title: "Clients"}); } }); } module.exports = 客户端路由配置;提前致谢!
【问题讨论】:
-
你的路由文件在哪里?
-
一个可能不相关的建议是使用 .service() 而不是 .controller() 创建您的服务。不确定这是否只是您在此处发布的代码中的拼写错误。
-
我不使用 Angular 路由。我正在关注本教程,他使用 jQuery 进行路由。我打算先使用它,然后在我了解有关 Angular 的更多信息时更改它。但是我已经添加了我的 jQuery 路由文件。最重要的是,我更改了“控制器”
-
我使用 service() 更改了服务文件中的控制器。这没什么区别。感谢您的建议,因为它改进了我的代码!此外,我不使用角度路由,我实际上使用 jQuery,因为我正在遵循的教程就是这样做的。我已经添加了 jQuery 路由。
标签: javascript jquery angularjs node.js