You can wrap your card inside a container div and then you can get the width of card using offsetWidth.
@ViewChild("myIdentifier") myIdentifier: ElementRef;
<div #myIdentifier>
<mat-card class="mat-elevation-z4">
<mat-card-header>
<mat-card-title>{{prof["fullName"].length > 15 ? prof['fullName'].slice(0, 15) + '...' : prof['fullName']}}
</mat-card-title>
</mat-card-header>
<img [class.hide]="width < 500" mat-card-image [src]="prof.imageLink" alt="Photo of a Shiba Inu">
</mat-card>
</div>
You need to use ngAfterViewInit event to get the width as only after view is rendered you can get the exact width.
ngAfterViewInit() {
this.width = this.myIdentifier.nativeElement.offsetWidth;
this.cdr.detectChanges();
}
Then based on your predefined width you can show hide the profile picture.
<img [class.hide]="width < 500" mat-card-image [src]="prof.imageLink"/>
As we updating the view after view initializated. we will get an error "Expression ___ has changed after it was checked".
To get rid of that error we need to manually invoke change detection.
this.cdr.detectChanges();
Edit1: Handle page resize.
If you want to make this responsive(on page resize) you need to detect/bind browser resize event and update the width property in that and it will work.
ngOnInit() {
this.resizeObservable$ = fromEvent(window, "resize");
this.resizeSubscription$ = this.resizeObservable$.subscribe(evt => {
this.width = this.myIdentifier.nativeElement.offsetWidth;
});
}
Make sure to destory the subscription in component onDestroy method.
ngOnDestroy() {
this.resizeSubscription$.unsubscribe()
}
Demo updated with latest code changes.