NewBee
NewBee

Reputation: 57

Angular 2: Undefined route parameter value

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

Upvotes: 0

Views: 1048

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222722

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;
                });
        }
    });
}

Upvotes: 1

Related Questions