2

I consider how effectively set in Angular 2+ width of element. I would like firstly to check available real estate in parent element and than appropriately set width of child elements to fit equally this available space in its parent.

  1. how could I listen on width change events of parent element?
  2. how to check this width?
  3. then I could make some calculation of elements width to fit requirements of component user
  4. then change width of element to calculated value.

I have tried to use CSS3 and Media Queries but it makes my Component to tightly coupled to certain sizing precondition and makes my angular component not general-purpose.

3
  • It is almost never a good idea to start trying to do your own layout calculations. And it is almost never necessary with the appropriate CSS, which can include percents, calc, and flexbox. Commented May 8, 2017 at 2:27
  • If you really need to, you could look at element queries. Commented May 8, 2017 at 2:41
  • Yeah but without my own calculation the angular component is not so easily reusable. Then its not just case of use it set max-cols number for my gird of cards and min-length required for this cards and it works, then each time for each use case you must manually do you own custom CSS to fit actual needs of the page. Then component isn't of general purpose, easy to use but its, just the first draft to start the designing of entirely new component based on current by modifying CSS files, etc. Commented May 8, 2017 at 8:45

3 Answers 3

4

You could use ngStyle on the child elements. The documentation's example:

<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>

You'd be responsible for retreiving/calculating the widthExp variable up-to-date from the parent component.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok I think I have achieved this
1

My solution finally was :

  1. add (windows:resize) event binding:
  2. and element reference #gridElement in HTML template

    <div class="grid" (window:resize)="onResize($event)" #gridElement>
    
  3. Then inside component class I need to Inject this guys:

    protected _changeDetectorRef:ChangeDetectorRef; 
    
    @ViewChild('gridElement') gridElement: ElementRef;
    
  4. Make sume helper variables

    public gridCardWidth = 0;
    public gridCols = 0; 
    
  5. 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.

  1. Then call this grid-card size adjusting function in this methods:

    ngOnInit(); ngAfterViewChecked(); onResize(); 
    
  2. In ngAfterViewChecked() I need to inform that adjusting of size has happened changeDetector object like this

    this._changeDetectorRef.detectChanges();
    
  3. Lastly I have a width style binding on grid-card element in HTML template

    <grid-card ... [style.width.%]="gridCardWidth"></grid-card>
    

Comments

0

Just use the below code in the component where you want.

this code is an example to set a div width from the component.

var x: any = document.getElementById('myDivID');
 x.style.width = '250px';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.