Angular JS 2.0切换样式表基于用户输入

时间:2016-04-28 09:04:20

标签: javascript angular angular2-template angular2-forms

如何根据AngularJS 2.0应用程序中的按钮单击切换样式表?

现在我在index.html页面的head部分包含样式表。

2 个答案:

答案 0 :(得分:1)

@Component({
  selector: ...,
  template: ...,
  styles: [`
:host(:not(.some-class)) {
  border: solid 1px red;
}
:host(.some-class) {
  border: solid 3px green; 
}

`
]})
export class MyComponent {
  @HostBinding('class.some-class') isSomeClass = true;
}

Plunker example

答案 1 :(得分:0)

我会利用ngStyle指令:

<h1 [ngStyle]="{'font-style': style, 'font-size': size, 'font-weight': weight}">
  Change style of this text!
</h1>
<div (click)="changeStyle()">Update style</div>

从方法(链接到点击事件)更新stylesize值将更新您的风格:

export class SomeComponent {
  style = 'normal';
  weight = 'normal';
  size = '20px';

  changeStyle($event: any) {
    this.style = 'italic';
 }