1

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
  • 1
    if you use class with condition then you can use ngClass For Eg. [ngClass]="{'text-success':true}" Commented Jan 28, 2021 at 9:22

1 Answer 1

4

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.

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

3 Comments

It's fine to call functions in your template, fyi.
@inorganik functions that do a lot of work can slow down your application considerably, that's why I said you should avoid them, if they're not nesscesary. I agree that it would make little to no difference here.
Using functions in template works but it will affect the performance of your application medium.com/showpad-engineering/…

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.