Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - *ngIf on string interpolated value fetched from web service

Tags:

I have to display two mutually exclusive buttons 'MARK COMPLETED' and 'COMPLETED'. If the task status is OPEN then 'MARK COMPLETED' needs to display, whereas if the task status is closed then 'COMPLETED' button needs to display.

<div *ngFor="#task of tasks" class="demo-updates mdl-card mdl-shadow--2dp mdl-cell mdl-cell--4-col mdl-cell--4-col-tablet mdl-cell--12-col-desktop">
    <div class="mdl-card__title mdl-card--expand mdl-color--teal-300">
    <h2 class="mdl-card__title-text">{{task.taskname}}</h2>
    </div>
    <div class="mdl-card__supporting-text mdl-color-text--grey-600">
    {{task.taskdesc}}  {{task.taskstatus}}
    </div>
    <div class="mdl-card__actions mdl-card--border">                
                <a href="#" class="mdl-button mdl-js-button mdl-js-ripple-effect">{{task.assignedto}}</a>


    <a [routerLink]="['/AllTasks']" *ngIf="{{task.taskstatus}}='OPEN'" class="mdl-button mdl-js-button mdl-js-ripple-effect" (click)="onClickMark(task)">Mark Completed</a>
    <a [routerLink]="['/CompletedTasks']" *ngIf="{{task.taskstatus}}='CLOSED'" class="mdl-button mdl-js-button mdl-js-ripple-effect">Completed</a>


</div>    

I tried *ngIf="{{task.taskstatus}}='OPEN'" , but it's not working. Any ideas ?

like image 845
user2180794 Avatar asked Jun 03 '16 08:06

user2180794


People also ask

Can I use interpolation in NGIF?

You can use interpolation to display the value of a variable in the component's class in the component's view template. Since showData is true, the <ul> list will be displayed. If you want to hide it, we just change the value of showData to false.

What is string interpolation in Angular 8?

String Interpolation in Angular 8 is a one-way data-binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view.

Can we use interpolation inside interpolation in Angular?

In Angular, you can use interpolation syntax for data binding for an expression as {{ expression}} . You can use inline expression or member variable inside {{ }} interpolation operator. Sometimes, you would like to use different interpolation characters instead of {{ }} for data binding. For example [ expession ].

What is the interpolation in Angular?

Interpolation refers to embedding expressions into marked up text. By default, interpolation uses the double curly braces {{ and }} as delimiters. Angular replaces currentCustomer with the string value of the corresponding component property.


1 Answers

* already indicates to Angular that it needs to process the value as expression. {{}} in expressions is invalid.

Use instead:

*ngIf="task.taskstatus=='OPEN'"

Also for compairson == instead of = (assignment) is required.

like image 135
Günter Zöchbauer Avatar answered Sep 19 '22 18:09

Günter Zöchbauer