1

I want to take the width of my table and then to use that width for the condition to applying styles dynamically based on width.
Example:

div class="nano-table-grid"
    [ngStyle]="{ tableCurrentWidth < 1200px ? 'flex: none; max-width: 75px;'}>

I have the problem to take that current width of the table.

2
  • 1
    Possible duplicate of How to get width of (DOM) Element in Angular2 Commented Jul 26, 2018 at 13:13
  • 1
    with this it is really hard to tell how you are computing/getting the width of that element. If you could post the other code that would help. Commented Jul 26, 2018 at 13:17

2 Answers 2

2

use the ngClass by creating a separate style class.

.sample{
  flex: none; 
  max-width: 75px;
}

div class="nano-table-grid"  [ngClass]="{'sample':  tableCurrentWidth < 1200px }>
Sign up to request clarification or add additional context in comments.

Comments

1
<div #table class="nano-table-grid"
  [ngStyle]="tableStyle(table.offsetWidth)"></div>
tableStyle(tableWidth: number) {
  return tableWidth > 1200 ? {
    display: 'block'
  } : {
    display: 'flex',
    maxWidth: '75px'
  }
}

You can instead use ngClass as @SachilaRanawaka suggested, both work.

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.