Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
ngIf condition in template not working while refresh, it's redirecting to homepage

I am working on Angular 7, and I am rendering a <div> ... </div> based on multiple values, these values I declared in the .ts file as : Boolean = false; the values will update in ngOnInit, but for my condition, it's keep redirecting back to the homepage while refreshing or first navigation to the page,

here my template

<div (click)="audioToggleSearchDropdown($event)" class="search-blk" id="audio-search" *ngIf="audioSearchKey || radioSearchKey || podcastSearchKey ">
            <input class="searchboxEvent" (keypress)="audioeventHandler($event)" #suggestbox id="search-audio-search"
              [formControl]="audioSearchContnet" [(ngModel)]="audioSearchValue"
              placeholder="{{ 'HEADER.AUDIO_SEARCH_PLACEHOLDER' | translate }}" />
 </div>

And my .ts file I declared the keys audioSearchKey , radioSearchKey podcastSearchKey as false,

audioSearchKey: Boolean = false;
radioSearchKey : Boolean = false;
podcastSearchKey : Boolean = false;

And in ngOnInit() I put a function to validate URL , and the function like,

if( url === "audio" || url === "search") {
  this.audioSearchKey = false;
  this.radioSearchKey = false;
  this.podcastSearchKey = false;
}else if (audioUrl.includes(url)) {
  this.audioSearchKey = true;
  this.radioSearchKey = false;
  this.podcastSearchKey = false;
} else if(radioUrl.includes(url)){
  this.audioSearchKey = false;
  this.radioSearchKey = true;
  this.podcastSearchKey = false;
} else if(podcastUrl.includes(url)){
  this.audioSearchKey = false;
  this.radioSearchKey = false;
  this.podcastSearchKey = true;
} else{
  this.audioSearchKey = false;
  this.radioSearchKey = false;
  this.podcastSearchKey = false;
}

After adding these, my existing page keeps redirecting to the homepage while clicking from a link and navigating to the page and while each refreshes. If am removing the HTML part with ngIf the rest of the functionalities and page working fine if anyone please suggest what error exactly it is?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Setting values to false that may be updated to true with ngOnInit is almost the same as setting them to true in your TS file. ngOnInit happens before the first DOM paint, you may want to try putting that update in ngAfterViewInit()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!