2

The background color of the div is not changing when using the style binding using Angular.The code is placed below for reference

@Component({
  selector: 'course',
  template: `
   <div [style.backgroundColor]='red'>

   <button (click)="onClick($event)" class="btn btn-primary" [class.active]="isActive" [style.backgroundColor]='red'>Save</button>
   </div>
    `,
    styleUrls: ['./course.component.css']
  })
  export class CourseComponent {...}

The style element has the backgroundColor property. Where am I going wrong?

1
  • do it in angular way using ngStyle Commented Feb 18, 2018 at 11:05

3 Answers 3

4

Angular property binding expects an expression instead of a value which you are trying to assign. Instead of [style.backgroundColor]='red' you might want to use [style.backgroundColor]="'red'" note that i have wrapped 'red' on quotes.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use [ngStyle] with backgroundColor property

<div  [ngStyle]="{backgroundColor: 'red' }">
   <button (click)="onClick($event)" class="btn btn-primary" [class.active]="isActive" [ngStyle]="{backgroundColor: 'red' }">Save</button>
</div>

DEMO STACKBLITZ

1 Comment

Broken link. I tried something like what you have with no luck.
0

I think you've a typo in your code ..

try this:

 [style.background-color]="'red'"

.. so something like:

@Component({
  selector: 'course',
  template: `
   <div [style.background-color]='red'>

   <button (click)="onClick($event)" class="btn btn-primary" [class.active]="isActive" [style.background-color]='red'>Save</button>
   </div>
    `,
    styleUrls: ['./course.component.css']
  })
  export class CourseComponent {...}

Hope it helps you!

2 Comments

No. Its not working. I tried <div [style.backgroundColor]='red'>
you have to try <div [style.background-color]='red'> as i say

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.