title: variable assigned is undefined angular typescript
body: <p>I have code as below. The constructor uses the router methods to get the previous route and sets a variable in it. Then ngOnInit() call a method which in turn calls another method to compare the variable saved through constructor with the list values from service. At this point when comparing the variable that is stored in contructor is always coming as undefined. However inside constructor the value og this.selBoard is 40. The value printed in console for 'console.log(this.selBoard);' isalways undefined. Where am i going wrong? Even though its an sync call, the value is printed properly which is 40 inthe constructor so there is no way its value is undefined inside getRouterDetails()</p>
  <pre><code>//...
  selBoard : number;
  selectedBoard : Board= new Board();
  
      constructor(private router: Router){
      this.router.events.pipe(filter((evt: any) =&gt; evt instanceof RoutesRecognized), pairwise())
              .subscribe((events: RoutesRecognized[]) =&gt; {
              var url = events[0].urlAfterRedirects;
              var urlIndex = url.indexOf('boardId=');
              var boardId = url.substring(urlIndex+8, urlIndex+10);
              this.selBoard = Number(boardId);
              console.log('selBoard --- '+this.selBoard);
              });
              
      ngOnInit(): void {
      //..
      if(this.loggedInUser.isAdmin)  {
          this.loadBoardsList();}
      ..//
      }
      
      
      loadBoardsList(){ 
              this._boardService.loadBoards().subscribe( posts =&gt;{
                  this.data = posts; 
                  console.log('loadBoardsList',this.data);
                  },
                  error =&gt; { 
                  console.log('loadBoardsList - error',error);
                 // this._errorService.handleError(error); 
                  this._messageService.add({severity:'error', summary:'Error Message', detail:error.error+' - '+error.message, sticky: true}); 
                  }, 
                  () =&gt; {  
                  this.boardsList = this.data.boardLi; 
                  this.getRouterDetails();
                  console.log(this.selectedBoard);
              });  
      }
      
      getRouterDetails(){
         for (let i = 0; i &lt; this.boardsList.length; i++) {
              console.log(this.boardsList[i].id);
              console.log(this.selBoard);
              if(this.boardsList[i].id == this.selBoard){
                  console.log('Which one selected ---'+this.selBoard);
                  this.selectedBoard = this.boardsList[i];
              }
          }
       }
  </code></pre>
  <p>Any suggestions much appreciated. thank you.</p>