Anusha
Anusha

Reputation: 73

Using ngIf in angular but I am getting Bindings cannot contain assignments

reservedcoders: ReservedCoders[];
export class ReservedCoders{
    constructor(
        public coderID:number
    ){ }
}

mycoder is an array

<td>
<div *ngIf="reservedcoders.find(s => s.coderID == mycoder.svUserID); else notEqual">
<button type="button" (click)="releasePopUp(mycoder.svUserID,mycoder.name)" class="btn btn-primary">Release</button>&nbsp;
<button type="button" (click)="addAdditionalPopUp(mycoder.svUserID,mycoder.name)" class="btn btn-primary">Add 10%</button>
</div>
<ng-template #notEqual>
<button type="button" (click)="releasePopUp(mycoder.svUserID,mycoder.name)" disabled class="btn btn-primary">Release</button>&nbsp;
<button type="button" (click)="addAdditionalPopUp(mycoder.svUserID,mycoder.name)" disabled class="btn btn-primary">Add 10%</button>
</ng-template>
</td>

Upvotes: 7

Views: 6455

Answers (1)

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

I'm not sure what the exact problem is, but I have found in the past that somewhat complex expressions do often fail in *ngIf bindings.

The likely best solution is to modify your code to call a simple method in your component, and implement the "find" in that method.

So, for instance, implement this in your component:

public coderIdIsReserved(): boolean {
  return this.reservedcoders.find(s => s.coderID === mycoder.svUserID) ;
}

and change your template to:

<div *ngIf="coderIdIsReserved(); else notEqual">

Upvotes: 7

Related Questions