theLearner theLearner - 5 months ago 29
AngularJS Question

filter 'ng-repeat' based on URL parameter

I wanna filter a ng-repeat based on a parameter from the URL

Say this is the URL-


So here I wanna filter on the Id.

ng-repeat="oneItem in listOfItems | filter : 27"

So the number 27 is to be got dynamically from the URL

Help me out with this guys, Lemme know if you need more info.. :)


I assume you are not using ng-router and 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. 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.

You can get the ID from url like this:

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.

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!