Angular 笔记 CSS 样式篇

今天就只是个笔记,单纯记录写法如何使用,以便自己日後查找。
毕竟人非圣贤、会忘难免 XD

css

套用 className

变数为 isDark,value 必需为 boolean

isDark = true;

在 html 的 dom 里设定

<p [ngClass]="isDark ? dark : light">test</p>
<p [ngClass]="{'isDark': true}">test</p>

https://angular.tw/api/common/NgClass

Document difference between [NgClass] and [class] [NgClass] 与 [class] 之间的差异


style

在 html 的 dom 里设定

<p
  [ngStyle]="{'background-color': '#159', 'color':'#fff', 'font-size': '20px'}"
>
  test
</p>
isDark = true;
<p [ngStyle]="isDark ? {'background-color': '#159', 'color':'#fff'} : {}">
  test
</p>

若 属性的值为 value

width = 60;
height = 20;
<p [ngStyle]="{'width.px': width, 'height.px': height}>test</p>

https://angular.tw/api/common/NgStyle


:host 设定宿主(host)的样式

:host {
  display: block;
  border: 1px solid black;
}

在 css 里加上条件式

:host(.active) {
  border-width: 3px;
}

@HostBinding

在 ts 里直接绑定 class 到 宿主上

@HostBinding() class = `hey`;

直接绑定 id 到 宿主上

@HostBinding() id = `hey`;

或是直接设定 style

@HostBinding() style = `background: #159;display: block;`;

套上变数

@Input() isColor!: string;

@HostBinding('style.background') get color() {
  return this.isColor;
}
@HostBinding('class.value') get color() {
  return true;
}

若是 return true,class 会挂载 value 这个 class name

@HostBinding('class') get color() {
  return 'yoo';
}

HostBinding 里的 只有 class,则需 return 出一个字串,return 出来的字串则为 class name


<<:  [Lesson20] ButterKnife

>>:  Day 23:开始来学资料系结:属性系结(二)

D12 - 用 Swift 和公开资讯,打造投资理财的 Apps { 加权指数K线图分析 }

K 线现在用在各种投资市场,不只是股市。最早从米市交易开始,现在任何有价格波动的地方,都会有人用 K...

DAY27:实作专案之动机及方向

今天先跟大家说明在我的学习过程中做出的一个小小专题,这个专题在当初还有跟三个同学共同完成,那就先说明...

笔记-标准系统格式

Standard System Independent Formats(标准系统独立格式) Gif ...

Day 30 Python3 + selenium 撷取网站状态快照

Python3 + selenium 撷取网站状态快照 系统更新与软件服务预装 $ sudo apt...

EP 02 - 文件阅读

Youtube 频道:https://www.youtube.com/c/kaochenlong ...