当前位置:   article > 正文

Angular4中[ngClass]、[ngStyle]的基本使用

ngstyle

1、ngStyle

基本用法

<div [ngStyle]="{'background-color':'green'}"></<div>
判断添加
<div [ngStyle]="{'background-color':username === 'zxc' ? 'green' : 'red' }"></<div>

2、ngClass

第一个参数为类名称,第二个参数为boolean值,如果为true就添加第一个参数的类

基本用法

[ngClass]="{'text-success':true}"

判断

  1. [ngClass]="{'text-success':username == 'zxc'}"
  2. [ngClass]="{'text-success':index == 0}"

3、例子循环显示的第一行添加text-danger样式,文字变红色

  1. const arr = [1, 3, 4, 5, 6]
  2. <ul>
  3. <li *ngFor="let item of arr, let i = index">
  4. <span [ngClass]="{'text-danger': i==0}">{{item}}</span>
  5. </li>
  6. </ul>

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/436135
推荐阅读
  

闽ICP备14008679号