I want to use a animation-delay dynamically inside a ngFor
loop. So I am using style="animation-delay:'i's;"
where i is the loop variable.
This is not working.
My code is like below:
<div class="yourdiv" *ngFor = "let item of gvp.userMessages;let i = index" [attr.data-index]="i"
style="animation-delay:'i's;">
<div style="text-align:right;margin-right:10px">
<ion-icon name="undo" class="rplymsg msgico"></ion-icon>
<ion-icon name="trash" class="delmsg msgico" (click)="deleteMessage()"></ion-icon>
</div>
<div *ngFor = "let msg of item[1]" class="{{msg.substring(0,1) == 'R' ? 'receivedmsg left-top' : 'replymsg right-top' }}">
{{msg.substring(2,msg.length)}}
</div>
</div>
Please help.
The basics. To declare a variable in CSS, come up with a name for the variable, then append two hyphens (–) as the prefix. The element here refers to any valid HTML element that has access to this CSS file. The variable name is bg-color , and two hyphens are appended.
You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a context object whose properties can be assigned to variables using let binding syntax. Note that you must specify an outlet for the template, but it can be a reference to itself.
In the template, you use the hash symbol, # , to declare a template variable. The following template variable, #phone , declares a phone variable with the <input> element as its value. Refer to a template variable anywhere in the component's template.
The let keyword in Angular declares a template input variable that is referenced within the template. In Angular, the micro syntax is used to configure a directive in a compact and friendly string.
Here it is, this is how you should write it :
[ngStyle]="{'animation-delay.s': i}"
Here is the working example of it, please have a look :
WORKING DEMO
For more details please read :
https://angular.io/api/common/NgStyle
[ngStyle]="{'max-width.px': widthExp}"
Note, that the accepted answer is out-dated. We should not use NgStyle
anymore (ref):
The NgStyle directive can be used as an alternative to direct [style] bindings. However, using the above style binding syntax without NgStyle is preferred because due to improvements in style binding in Angular, NgStyle no longer provides significant value, and might eventually be removed in the future.
Instead use direct style-bindings as explained in Ron Newcombs answer
Shorthand for most cases:
[style.animation-delay.s]="i"
or
[style.max-width.%]="propertyOnController"
for
@Component({...})
export class ControllerComponent {
propertyOnController: number;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With