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

123
Views
Angular render component based on device width

What I'm trying to accomplish is to display the appropriate view / component based on the device width. I have attached an event listener and created an if statement to render the appropriate ng-template based on a boolean class flag mobile, however when I drag my browser the template doesn't change but the resize event does fire...

html

<div class="row dashboard-list-view">
  <div class="col-12" *ngIf="mobile; then mobileListBlock else desktopListBlock"></div>
  <ng-template #desktopListBlock>
    <app-dashboard-list-desktop [data]="data" [filters]="filters"></app-dashboard-list-desktop>
  </ng-template>
  <ng-template #mobileListBlock>
    <app-dashboard-list-mobile [data]="data" [filters]="filters"></app-dashboard-list-mobile>
  </ng-template>
</div>

ts

import { Component, Input, OnInit } from '@angular/core';
import { ResizeService } from '../../services/resize.service';

@Component({
  selector: 'app-dashboard-list',
  templateUrl: './dashboard-list.component.html',
  styleUrls: ['./dashboard-list.component.scss']
})
export class DashboardListComponent implements OnInit {

  @Input() data: any = [];
  @Input() filters: any = [];
  
  mobile: boolean = false;

  constructor(private resizeService: ResizeService){}

  objectKeys = Object.keys;

  ngOnInit() {

    this.resizeService.onResize$.subscribe(this.onWindowResize);
    this.onWindowResize();

  }

  onWindowResize() {

    console.log("Resize!")

    if (window.innerWidth < 992) {
      this.mobile = true;
    } else {
      this.mobile = false;
    }

  }

}

I've looked for similar questions but I cant figure out what I'm doing wrong here, thank you in advance!

Update

However when I initialize the app on a mobile device it does render the mobile view. Same thing for desktop too. It's just that the event doesn't have any effect after the initial render.

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

0

Well the problem was this line:

this.resizeService.onResize$.subscribe(this.onWindowResize);

changing it to:

this.resizeService.onResize$.subscribe(() => this.onWindowResize());

did the trick. It prolly had to do with the this context on the member function. No idea why no error was thrown tho...

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!