My solution finally was :
- add (windows:resize) event binding:
and element reference #gridElement in HTML template
<div class="grid" (window:resize)="onResize($event)" #gridElement>
Then inside component class I need to Inject this guys:
protected _changeDetectorRef:ChangeDetectorRef;
@ViewChild('gridElement') gridElement: ElementRef;
Make sume helper variables
public gridCardWidth = 0;
public gridCols = 0;
Write function adjusting size of each grid-card element
adjustCardWidthToAvailableEstate() {
var gridWidth : number = this.gridElement.nativeElement.offsetWidth;
var newGridCardWidth = this.calculateCardWidth(gridWidth);
this.gridCardWidth = ((newGridCardWidth - this.GRID_CARD_MARGIN_LEFT)/gridWidth)*100; // [%]
this.gridCols = Math.floor(gridWidth/this.gridCardWidth);
// refresh list of cards based on content objects
this.contentObjectsObservable = Observable.of(this.contentObjects);
}
Important here is this line of getting grid container width:
var gridWidth : number = this.gridElement.nativeElement.offsetWidth;
The rest is just the logic of calculation.
Then call this grid-card size adjusting function in this methods:
ngOnInit(); ngAfterViewChecked(); onResize();
In ngAfterViewChecked() I need to inform that adjusting of size has happened changeDetector object like this
this._changeDetectorRef.detectChanges();
Lastly I have a width style binding on grid-card element in HTML template
<grid-card ... [style.width.%]="gridCardWidth"></grid-card>
calc, and flexbox.