javascript - 使用 Express 捕获参数

标签 javascript angularjs node.js express

我使用express来路由页面,而不是Angular-route(我正在开发一个利用Angular、Node、Sequelize和Express的Web应用程序)。到目前为止,路由与 Angular 配合得很好,一切都很好。但是,现在我将一个参数传递到 url 中,并且我想在目标页面上利用该参数来按此参数过滤结果。不幸的是,我只是获取页面上的所有客户端,而不是基于参数的过滤列表。这是我的express.js 的一小部分:

app.get('/client/:id', routes.clientpage, function(req, res) {
var id = req.param('id');
res.send(id);
console.log('req.param('id')');
}
);

} 这是我尝试使用此参数“id”的页面:

<!DOCTYPE html > 
<html ng-app="app">
<%include header3%>
<%include navbar%>
<div ng-controller="ClientCtrl">
<div ng-repeat="client in clients | filter:{id:id} ">
<header><a href="/about" class="btn btn-primary">Back to clients</a>  <h2>{{client.name}}</h2> <small> <span style="padding-left:463px">Staff: {{client.staff}} </small> </header>
<h1><span style="padding-left:300px"><a href="/client/{{client.id}}" class="btn btn-primary">Personal</a>
<a href="/client/{{client.id}}/ci" class="btn btn-primary">Case Info</a>
<a href="/client/{{client.id}}/i" class="btn btn-primary">Insurance</a>
<a href="/client/{{client.id}}/m" class="btn btn-primary">Medical</a>
<a href="/client/{{client.id}}/f" class="btn btn-primary">Financial</a></h1>
<body>
<div class="container">
<div ng-controller="ClientCtrl">
<div class="datagrid"><table>  
<thead> 
<tr> 
 <th> ID </th>
 <th> Phone </th>
 <th> Address </th>
 <th> Zip </th>
 </tr>
 </thead>
 <tbody>
 <tr ng-repeat="client in clients | orderBy:'id' | filter:{id:id} | limitTo: 1">
 <td>
 {{client.id}}
 </td>
 <td> <input type="text" ng-model="client.phone" value="{{client.phone}}"/> </td>
 <td><input type="text" value="{{client.address}}"/></td>
 <td><input type="text" value="{{client.zip}}"/></td>
 </tr>
 <tr> 
 <thead>
 <th> SSN </th>
 <th> Age </th>
 <th> DOB </th>
 <th> DLN </th>
 </thead>
 </tr>
 <tr ng-repeat="client in clients | filter:{id:id} | orderBy:'id' | limitTo: 1">
 <td> <input type="text" value="{{client.ssn}}"/> </td>
 <td><input type="text" value="{{client.age}}"/</td>
 <td><input type="text" value="{{client.birthday}}"/></td>
 <td><input type="text" value="{{client.dln}}"/></td>
 </tr>
</tbody>
</table>
</body>

最佳答案

捕获 GET 参数的正确方法是这样的:

app.get('/client/:id', routes.clientpage, function(req, res) {
  var id = req.params.id;
  res.send('id : ' + id);
  console.log(id);
});

关于javascript - 使用 Express 捕获参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37330097/

相关文章:

javascript - kendo ui 事件(或来自其他框架的事件)是 Javascript 事件吗?

javascript - 你如何设计像 DIV 这样的自定义元素?

javascript - 如何使用 ng-repeat 同时获取两个连续的数组对象

javascript - 找不到名称 'ENV' returnTo : ENV. BASE_URI

node.js - Couchdb 套接字在 View 中挂起

javascript - Javascript 是编译还是两次解释?

javascript - 如何在 Javascript 中基于另一个数组获取多个数组值

angularjs - 如何使用AngularJS通过单击来div隐藏和显示

javascript - 如何在node.js中使用多语言邮件模板?

javascript - Zend 1.12 : Append javascript to bottom of view