3

Background

In my template file I am looping through some data from a http response.

<div *ngFor="let method of paymentMethods">

Within that loop I am outputting

method?.payment_profile_id

Now inside of that loop I want to show one of two elements based on the value of method?.payment_profile_id.

I am trying to express this inside of a *ngIf

*ngIf="defaultPayment = method?.payment_profile_id"

Or,

*ngIf="defaultPayment != method?.payment_profile_id"

But when trying to do that, I get a parser error,

Parser Error: Bindings cannot contain assignments at column 18 in [defaultPayment = method?.payment_profile_id]

Question

What is the proper way to write this expression?

wuno
  • 9,547
  • 19
  • 96
  • 180
  • 2
    Use a double equals, not a single, `==`, a single `=` is an assignment operator, not an equality operator. – tymeJV Mar 14 '17 at 18:27
  • Possible duplicate of [Angular 2 expression parser and ng-init directive](http://stackoverflow.com/questions/39210257/angular-2-expression-parser-and-ng-init-directive) – Jason Goemaat Mar 17 '17 at 08:47

2 Answers2

4

You are doing assignment with = but it should be == or === if you want comparison.

Hung Cao
  • 3,130
  • 3
  • 20
  • 29
0

As it is clear from the error you got

Bindings cannot contain assignments at column 18 in [defaultPayment = method?.payment_profile_id]

*ngIf (binding) is just used to check and return Boolean(true/false) to the template you cannot assign any value between *ngIf. so to check the equality you either have to use == or === not = so the correct syntax is as stated in above answer.

*ngIf="defaultPayment == method?.payment_profile_id"
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215