Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
Angular ngStyle Parser Error: Unexpected token [, expected identifier or keyword

I need to be able to pass a dynamic string using the fieldName in order to get an attribute within the item object. In order to make it easy to duplicate the issue I have given a small example. The problem is this syntax works properly with angular 12.2.5 as I checked but did not work with angular 12.0.0 or below. I can simply call a function but I know this is not good for performance. Is there an alternate way to write this syntax for older angular applications?

HTML

<p [style.background]="item?.[fieldName]">
  Some Text
</p>

ts

item =  { warn: 'yellow', success: 'green', error: 'red' };
fieldName = 'success';

Error looks like this

Parser Error: Unexpected token [, expected identifier or keyword at column 20 in [{background: item?.[fieldName]}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try to use ternary operator ?::

<p [style.background]="item[fieldName] ? item[fieldName] : ''">
  Some Text
</p>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!