1

I am building an application (using express, node, and angular) that stores clients and has the ability to view their info. Each client is assigned an id key. I have created a link such that when the client's name is clicked, it goes to a url that is like so : client/{{client.id}}. I want to extract this parameter, id, so I can filter the clients and show only the one with that id. Here is my express.js, where I find the parameter id and print it in console:

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

Now, I see the id printed in console log -- but, using Angular, how would I inject this parameter into a filter? This is what I think, but I evidently wrong :

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

1 Answer 1

2

You can do it whi some url parsing expression like this:

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

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

That will work for urls like myapp.com/example/client/46235, but may fail for myapp.com/example/client/46235/getsmth so you may want to use other solutions:

  • angular UI router for client side app naviagtion
  • Output this ID in some part of you server generated template.
Sign up to request clarification or add additional context in comments.

6 Comments

Will you please explain as to how document.location.href.split('client/')[1]; achieves the effect that is desired ?
document.location.href.split('client/')[1] uses string split function (developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…) - will take the url of the page (say for example full url is 'myapp.com/example/client/46235') and will return you the last part - '46235'. Then you can assign it to some scope variable ($scope.clientId for example) and the use it in the filter expression.
thank you, I am having an issue with this at the moment. Do you think you would be able to advise?
can you be more specific, what's the problem?
Okay, so the routing works fine and when I click on a client I am led to the client page with the specified client's info. I have four buttons that lead to other pages that show different info of the client. When I click on one of these buttons, say case information, it always leads to me the client with id of 2, not the id of the parameter. The link url that I am using is ci/{{client.id}}.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.