My attempts:
<div ng-class="{'test': true}">Static key works fine</div>
<div ng-class="{ ['te'+'st']: true }">How to "Create object with dynamic key in Angular expression"?</div>
<div ng-class="{ 'te'+'st': true }">Attempt #2</div>
<div ng-class="{ `te${'st'}`: true }">Attempt #3</div>
You can view it at this plunk.
Edit 1:
To make it crystal clear: I'm not looking for a way how to add dynamically classes on an element. Code above is just a minimal example of what I want to achieve - construct an object in Angular expression with dynamic keys. Consider ng-class
to be just some arbitrary directive taking an object as a parameter, it's used just for demonstration and testing.
In JavaScript (ES6) it can be achieved like this: {['te'+'st']: true}
which produces object (not string) {test: true}
.
In production code first part of expression is variable and the other is string, condition (value) wasn't hardwired true
and also object is not composed of just one property. But I don't think it really matters, because question is about object and dynamic keys in ng expression and it IMO implies it's not restricted to only string literals concatenation (it wouldn't be very dynamic, would it).