Assign dynamic class in Angular

Viewed 52

I am trying to assign a height class like this:

<div class="getScrollViewHeight()">

And my method looks like this:

getScrollViewHeight(): string {
        return 'h-'+this.scrollViewHeightPercentage;
    }

What would be the correct way to do this? And can I use ngClass here?

1 Answers

You can just use

<div class="{{ 'h-' + scrollViewHeightPercentage }}">

As a sidenote, using functions in your template can potentially mean that it is called very often, depending on how often angular has to check for changes. You should avoid calling functions in your template.

Related