Bring id on route

Asked

Viewed 326 times

5

I’m doing a user management. At the moment, I’m creating the put to edit. But, I don’t know how to bring the id on the route.

There is a list of users and also an insert. When the client clicks on the button to recover the password, I would like it to already bring the user id and so put.

This is the update (user.id is coming Undefined because the route is not bringing):

update(user: User) {
        this.endpoint = '/users/' + user.id;
        return this.service.put(this.endpoint, user);
    }

And so the put:

put(path: String, params) {
        return this.http.put(this.url + path, params, this.standardHeaders()).map((response: Response) => {
            return response.text().length > 0 ? response.json() : {};
        });
    }

1 answer

1


Normally this is done as follows:

constructor(private route: ActivatedRoute)
userId=0;
ngOnInit() {
 this.route.paramMap
      .subscribe((params: ParamMap) =>  this.userId=params.get('id'))
    //id igual configurdo na sua rota por exemplo /user/:id
};

update(user: User) {
   this.endpoint = '/users/' + this.userId;
   return this.service.put(this.endpoint, user);
}

ps: it is best to have the endpoint in the service and only pass the id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.