Angular 无法使用样式绑定更改角度中的背景色

Angular 无法使用样式绑定更改角度中的背景色,angular,Angular,使用Angular样式绑定时,div的背景色不会改变。代码放在下面以供参考 @Component({ selector: 'course', template: ` <div [style.backgroundColor]='red'> <button (click)="onClick($event)" class="btn btn-primary" [class.active]="isActive" [style.backgroundColor]='re

使用Angular样式绑定时,
div
的背景色不会改变。代码放在下面以供参考

@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 {...}
@组件({
选择器:“课程”,
模板:`
拯救
`,
样式URL:['./course.component.css']
})
导出类CourseComponent{…}

style元素具有
backgroundColor
属性。我哪里做错了?

我想你的代码有误

试试这个:

 [style.background-color]="'red'"
。。比如:

@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 {...}
@组件({
选择器:“课程”,
模板:`
拯救
`,
样式URL:['./course.component.css']
})
导出类CourseComponent{…}

希望它能帮助你

您需要将
[ngStyle]
backgroundColor
属性一起使用

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

拯救

角度属性绑定需要一个表达式,而不是您试图分配的值。
您可能需要使用
[style.backgroundColor]='red'
而不是
[style.backgroundColor]='red'
注意,我在引号中包装了'red'。

否。它不起作用。我试过
你必须像我说的那样试着用NGSTYLEBREAK链接以有角度的方式做。我尝试了一些你没有运气的东西。