Bring id on route

5

I'm doing a user management. At the moment, I'm creating the put to edit. But, I do not 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 thus put the put.

So the update (the 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 here's 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() : {};
        });
    }
    
asked by anonymous 01.11.2017 / 19:55

1 answer

1

Usually 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 better practice to have the endpoint in the service and just pass the id.

    
13.03.2018 / 18:09