0

I am new to angular and please correct me if my understanding in terminology are incorrect.

Working on a sample app where I need to read the route parameter value in child component . I am able to print the "Params" object but unable to extract the data in it. Just pasting my code so that I can give more details :

Customer Details component:

 ngOnInit() {

        this.route.parent.params.subscribe((params: Params) => {
           console.log( params);
        console.log("Id value" + params['id']);

       if (1) {
    this._custServ.getCustomerDetailsById(1)
      .subscribe((customer: ICustomer) => {
        this.customer = customer;
       //s console.log(customer.id);

      });
  }
});

}


Url I am accessing : http://localhost:4200/customers/1/details


Google Chrome Console:

messages on console

I have tried to get the parameters value since a day but no luck. Could you please guide me in this case.

Thanks Prashant


Updated code as per the guidance from Sujeethan but now I am receiving id as null value. full code of the component below. Code

1 Answer 1

1

Anyhow your console showes the correct value, it just that your if condition is wrong, you should assign the value and check in if condition

ngOnInit() {
    this.route.parent.params.subscribe((params: Params) => {
        console.log("Id value" + params['id']);
        let id = params["id"];
        if (id ==="1") {
            this._custServ.getCustomerDetailsById(1)
                .subscribe((customer: ICustomer) => {
                    this.customer = customer;
                });
        }
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Sajeetharan , I updated the code but I am now receiving null value. Message on console: "Id value null "
Hi Sajeetharan, i used your same code but still seeing id value as undefined. Any thoughts whether I am missing anything?
I just copied your code and pasted in VS editor and checked the results.. Unfortunately it is still undefined value.
with the latest image you provided i see still if(1)
Thank you Sanjeethan for your help :) The issue was a typo mistake in the routing file : I had put a space after id and Sanjeethan kindly had a screen share session with me and identified and fixed as below. Silly mistake but learnt a lesson:) { path :'customers/:id' , component : NewCusomerComponentComponent, children : [{path :'details' ,component : CustomerDetailsComponent}] }

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.