P
P
Pavel2017-11-19 05:41:27
Angular
Pavel, 2017-11-19 05:41:27

Angular5, why is the function called 2 times in ngClass?

I am mastering Angular and do not yet understand all the features and subtleties.
In particular, this example:
there is a regular component (let's say I have a list item) and it has a method that returns an array of css classes:

cssGetClasses() {
    console.log('call cssGetClasses');

    let css = [];
    // здесь идут какие-то условия и css заполняется исходя из них
    if (true) {
        css.push('ok');
    } else {
        css.push('err');
    }

    return css;
}

And the template for ngClass specifies this method: so that's why this "cssGetClasses" method is called twice? If suddenly I'm designing something wrong, then what is the best way to automatically generate css classes for an element when the component's properties change?
<li [ngClass]="cssGetClasses()"></li>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question