Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use variable inside style tag Angular 2

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.

like image 466
Arijit Avatar asked Oct 27 '17 02:10

Arijit


People also ask

How do you use variables in style?

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.

How to use variable in HTML in Angular?

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.

How to declare a variable in a template in Angular?

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.

What is let in angular?

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.


3 Answers

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}"
like image 61
Vivek Doshi Avatar answered Oct 31 '22 15:10

Vivek Doshi


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

like image 33
TmTron Avatar answered Oct 31 '22 15:10

TmTron


Shorthand for most cases:

[style.animation-delay.s]="i"

or

[style.max-width.%]="propertyOnController"

for

@Component({...})
export class ControllerComponent {
    propertyOnController: number;
like image 41
Ron Newcomb Avatar answered Oct 31 '22 15:10

Ron Newcomb