Make your component look like this :
You must have called your function from constructor
.
You need to get ID and call the function from ngOnInit
.
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-comp',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private route: ActivatedRoute) {
this.myFunction(this.myID); // Remove this line from constructor
}
ngOnInit() {
this.route.params.subscribe(params => {
this.myID = params['id'];
this.myFunction(this.myID); // Add your function call in ngOnInit
});
}
}
To know the difference between constructor
and ngOnInit
please click here