I am new to Angular and Typescript. I am trying to set boolean variable to true if an object is found in an array of objects based on id. The method shown below is called from ngOnInit().
getTheatre(){
this.mChandigarh=false;
this.mBangaluru=false;
this.mChennai=false;
this.flag="Came to method call";
for(let i=0; i<this.mChandigarhArr.length; i++){
this.flag1="Came to chandigarh index"+i;
if(this.mChandigarhArr[i].movieid===this.movie.id){
this.flag2="ids matched";
this.mChandigarh=true;
break;
}
}
}
The array mChandigarhArr has the element which matches the given condition , hence the mChandigarh variable should be set to true.
However the code does not seem to go in the loop itself. The flag gets shown in UI, but flag1 and flag2 are not at all shown.
Earlier i had tried using mChandigarhArr.findIndex() as well. It did not work out for me.
<<=== Adding ngOnInit() code=====>
ngOnInit() {
this.getChandigarhTheatre();
console.log(this.mChandigarhArr); //Shows the array is empty here
this.getTheatre(); // If i call this method from a click event in template then it works
}
getChandigarhTheatre(){
this.movieService.getChandigarhMovies().subscribe(
theatres => this.mChandigarhArr=theatres,
err => this.errMsg = <any>err
);
}