4

With this binding

css: 'item' + $index()

I get this output

class="item0"
class="item1"
class="item2"
...

I'm not able to use this when binding multiple classes like below, a syntax error is thrown because : is expected instead of +. Any hints?

css: { 'item' + $index(), 'active': $index() == 0 }

A workaround is to use attribute binding, but if possible I'd like to avoid that because there are existing classes set already, so I'd have to include those in the binding.

attr: { 'class': 'slider-item item' + $index() + ($index() == 0 ? ' active' : '') }

1 Answer 1

8

You can also do the same string concatenation in the css binding:

css: 'item' + $index() + ($index() == 1 ? ' active' : '')

Demo JSFiddle.

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

2 Comments

Didn't think of that, it works indeed and it's cleaner. +1 for now, will accept if it turns out to be the best possible solution.
If you want to set dynamic class like item0, item1 etc. you cannot use an object literal as the css binding parameter, so your only options is to use string concat to build your classes either in attr or in the css binding.

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.