2016-05-23 83 views
0

我正在构建一个存储客户端并能够查看其信息的应用程序(使用express,node和angular)。每个客户端都分配一个ID密钥。我创建了一个链接,当点击客户端名称时,它会转到如下所示的url:client/{{client.id}}。我想提取这个参数,id,所以我可以过滤客户端并只显示那个id。这里是我的express.js,在那里我找到参数ID和控制台打印:如何使用AngularJs将URL中的参数注入到HTML中

// express stuff 
app.param('id', function (req, res, next, id) { 
console.log(req.params.id); 
next(); 
}); 
app.get('/client/:id', routes.clientpage); 

现在,我看到了编号印在控制台日志 - 但是,采用了棱角分明的,我怎么会注入这个参数为过滤器?这就是我的想法,但我显然错了:

<ng-repeat="client in clients | orderBy:'id' | filter:{id:{{$req.params.id}}} | limitTo: 1"/>

回答

1

你可以做到这一点WHI的一些URL解析表达式是这样的:

//in controller 
$scope.clientId = document.location.href.split('client/')[1]; 

//in template 
<ng-repeat="client in clients | orderBy:'id' | filter:{id:clientId} | limitTo: 1"/> 

这将像myapp.com/example/client/46235网址的工作,但可能会失败为myapp.com/example/client/46235/getsmth所以你可能需要使用其他的解决方案:

  • angular UI router的客户端应用程序naviagtion
  • 在服务器生成的模板的某个部分输出此ID。
+0

请问您如何解释document.location.href.split('client /')[1];达到期望的效果? –

+0

document.location.href.split('client /')[1]使用字符串拆分函数(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) - 将采取网页的URL(例如说完整的网址是'myapp.com/example/client/46235'),并将返回最后一部分 - '46235'。然后,您可以将它分配给某个范围变量(例如$ scope.clientId),并在过滤器表达式中使用它。 – shershen

+0

谢谢,目前我遇到了这个问题。你认为你可以提供建议吗? –

相关问题