[英]filter 'ng-repeat' based on URL parameter
I wanna filter a ng-repeat based on a parameter from the URL 我想根据网址中的参数过滤ng-repeat
Say this is the URL- 假设这是网址-
http://sitename/pagename.aspx?Id=27
So here I wanna filter on the Id. 所以在这里我想过滤ID。 like-
喜欢-
ng-repeat="oneItem in listOfItems | filter : 27"
So the number 27 is to be got dynamically from the URL 因此,要从URL动态获取数字27
Help me out with this guys, Lemme know if you need more info.. :) 帮我解决这个问题,Lemme知道您是否需要更多信息。:)
I assume you are not using ng-router
and ui-router
. 我假设您没有使用
ng-router
和ui-router
。
It is a bad practice to filter things in the frontend. 在前端过滤内容是一种不好的做法。 Rather, you should send that
ID
to your backend and return the correct result. 相反,您应该将该
ID
发送到后端并返回正确的结果。 Then, you could simply do: 然后,您可以简单地执行以下操作:
ng-repeat="oneItem in listOfItems"
You can do this by changing the method that returns a result which then assigned to listOfItems
. 您可以通过更改返回结果然后分配给
listOfItems
的方法来listOfItems
。
You can get the ID from url like this: 您可以像这样从网址获取ID:
var url = window.location.href;
$scope.productId = url.split("=")[1] // this assumes that url is exactly like you wrote
Then, add this to your $http
get request and get the data from your backend. 然后,将其添加到
$http
get请求中,并从后端获取数据。
If you still insist on doing it in the front end, do this: 如果您仍然坚持在前端执行此操作,请执行以下操作:
ng-repeat="oneItem in listOfItems | filter : {id: productId}" // this assumes that oneItem object has property of id
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.