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

127
Views
Componente de representación angular basado en el ancho del dispositivo

Lo que estoy tratando de lograr es mostrar la vista/componente apropiado según el ancho del dispositivo. Adjunté un detector de eventos y creé una declaración if para representar la ng-template adecuada basada en un mobile de bandera de clase booleana, sin embargo, cuando arrastro mi navegador, la plantilla no cambia, pero el evento de cambio de tamaño se activa ...

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>

t

 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; } } }

He buscado preguntas similares, pero no puedo entender qué estoy haciendo mal aquí, ¡gracias de antemano!

Actualizar

Sin embargo, cuando inicializo la aplicación en un dispositivo móvil, muestra la vista móvil. Lo mismo para el escritorio también. Es solo que el evento no tiene ningún efecto después del renderizado inicial.

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

0

Bueno, el problema era esta línea:

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

cambiándolo a:

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

Hizo el truco. Probablemente tenía que ver con this contexto en la función miembro. No tengo idea de por qué no se arrojó ningún error aunque ...

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!