我想知道,當我們在css檔案中定義了一個類時,如何改變點擊的顏色? 我們應該使用ngClass還是ngStyle?
謝謝你。
Css檔案
.text-color: {
color:red;
}
html
<div>
<p>
一些文本...
</p>
</div>
uj5u.com熱心網友回復:
你可以直接系結HTML屬性,而不是:
<div(click)="divClicked = ! divClicked">
<p [class.text-color]="divClicked">
一些文本...
</p>一些文本。
</div>/span>
在你的組件中,創建類屬性來追蹤點擊的狀態:
divClicked = false
uj5u.com熱心網友回復:
codeSolution:https://stackblitz.com/edit/ngstyle-examples-m7sifc?file=src/app/app.component.html
html
<p [ngStyle] ="{ color: colorFlag }">top</p>
<button(click)="change()"> 點擊</button>
ts
import { Component } from '@angular/core'/span>;
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'] 。
})
export class AppComponent {
colorFlag = 'red';
change(){
this.colorFlag= 'green'。
}
闡釋: 在點擊按鈕時,p標簽的顏色將變為綠色,默認為紅色
。uj5u.com熱心網友回復:
如果你使用的是類,合規的解決方案:
<div
<p [ngClass]="{'text-red': true, 'text-white': false}">
一些文本...
</p>一些文字。
</div>/span>
在你的css檔案上有類
.text-red: {
color:red;
}
.text-white: {
color:white;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/321642.html
標籤:
上一篇:使SVG具有回應性的問題
