I have a controller implements OnInit The problem here is whenever i change the route and come back to same component ngOnInit is called everytime. What i am doing wrong i am not able to understand.Anybody please help me.
@Component({
selector:'test-list',
templateUrl:'./testlist.component.html',
styles:[`.testname{
text-transform : capitalize;
}`]
})
export class TestListComponent implements OnInit{
testList:Array<Test>;
constructor(private testService:TestService,private router:Router){}
ngOnInit(){
this.testService.getTest()
.subscribe(
data=>this.testList = <Array<Test>>data,
error=>alert(error)
);
console.log("ngInit")
}
editTest = (id)=>{
this.router.navigate(['createtest',id]);
}
}
TestListComponentsoOnInit()call is expected. - penleychanIt's a new instance every time component is called- penleychanIt should be called only once as i know when the TestListComponent is created not every time when i come back to same component. Am i right?and notcomponent is created every time when we change the route?. - Aakash Thakuras Array<Test>? <Array<Test>>data .... is the outer <> necessary? if your return type is as I suggested this.testList = data would suffice - JGFMK