Using ngClass for conditional styling, here is the usage from the docs:

/**
* @ngModule CommonModule
*
* @usageNotes
* ```
* <some-element [ngClass]="'first second'">...</some-element>
*
* <some-element [ngClass]="['first', 'second']">...</some-element>
*
* <some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
*
* <some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>
*
* <some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>
* ```
*
* @description
*
* Adds and removes CSS classes on an HTML element.
*
* The CSS classes are updated as follows, depending on the type of the expression evaluation:
* - `string` - the CSS classes listed in the string (space delimited) are added,
* - `Array` - the CSS classes declared as Array elements are added,
* - `Object` - keys are CSS classes that get added when the expression given in the value
* evaluates to a truthy value, otherwise they are removed.
*
* @publicApi
*/

It is also recommended when the conditional logics is complicated, we can using Function:

<div [ngClass]="getCondClass(i)" *ngFor="let item of items as collection; index as i; first as isFirst; last as isLast; even as isEven; odd as isOdd;">
{{i + 1}}: {{item}} -- {{collection}}
</div>
  getCondClass (index) {
if (index + === ) {
// Using array to add 'third', 'very*important' classes
return ['third', 'very-important'];
} if (index === this.items.length - ) {
// Using string to add class
return 'last';
} if (index === ) {
// Using object to add class
return {'first': true};
}
}

[Angular] ngClass conditional的更多相关文章

  1. angular ng-class使用笔记

    在前面Angularjs开发一些经验总结中说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJO( ...

  2. Angular - - ngClass、ngClassEven、ngClassOdd、ngStyle

    这几个都关于样式及类名修改的,所以先把样式代码贴上吧. .red{color:red} .blue{color:blue} 写案例用到的样式就这么简单的两个,下面进入正题. ngClass ngCla ...

  3. angular [NgClass] [NgStyle],NgIf,[ngSwitch][ngSwitchCase]

    [NgClass]  CSS 类会根据表达式求值结果进行更新,更新逻辑取决于结果的类型: string - 会把列在字符串中的 CSS 类(空格分隔)添加进来, Array - 会把数组中的各个元素作 ...

  4. angular -- ng-class该如何使用?

    ng-class是一个判断是否给某一个元素添加类名的属性: 例如:下面是判断 是否添加 aHover 这个类名: <ul class="nav fl w120 o"> ...

  5. AngularJS 的表单验证

    最近开始学习angularjs,学到表单验证的时候发现有必要学习下大神的好文章: 转:http://www.oschina.net/translate/angularjs-form-validatio ...

  6. 一步一步弄懂angularJS基础

    问题1:ng-app指令的使用以及自定义指令 <!doctype html> <!--这里的ng-app的属性值就是模块的名称,也就是 angular.module("My ...

  7. 浅谈Angularjs至Angular2后内置指令的变化

    一.科普概要说明 我们常说的 Angular 1 是指 AngularJS: 从Angular 2 开始已经改名了.不再带有JS,只是单纯的 Angular: Angular 1.x 是基于JavaS ...

  8. angular学习之关于ng-class详解

    1,定义和用法 ng-class 指令用于给 HTML 元素动态绑定一个或多个 CSS 类. ng-class 指令的值可以是字符串,对象,或一个数组. 如果是字符串,多个类名使用空格分隔. 如果是对 ...

  9. angular的ng-class

    项目内想到要替换class时,第一反应是使用angular最为简单粗暴的class改变方式: 在angular中为我们提供了3种方案处理class:        1:scope变量绑定,如上例.(不 ...

随机推荐

  1. Codeforces 934.D A Determined Cleanup

    D. A Determined Cleanup time limit per test 1 second memory limit per test 256 megabytes input stand ...

  2. Linux下kill命令

    首先了解什么是信号:信号是进程级的中断请求,系统定义了30余种信号,kill是管理员用来发送信号的一种手段. 功能说明:删除执行中的程序或工作. 语 法:kill [-s <信息名称或编号> ...

  3. WKWebview 和 WebViewJavascriptBridge

    WKWebview 和 WebViewJavascriptBridge https://www.cnblogs.com/L-vincen/p/6681435.html 链接在这里,有很多不错的文章,大 ...

  4. 【CF1023D】Array Restoration(构造,线段树)

    题意:有一个长为n的序列,对其进行q次操作,第i次操作可以把连续的一段覆盖为i 现在给出操作后的序列,第i个数字为a[i],其中有一些为0的位置可以为任意值,要求构造任意一组合法的操作后的序列 无解输 ...

  5. DataTable 去重合并

    //合并 dt.Merge(dt2); //去重 dt = dt.AsDataView().ToTable(true);

  6. Zabbix监控服务器异常IP登录

    保存下面的脚本到任意路径 名字.py 修改属组和权限: chown zabbix:zabbix * chmod 755 * 演示的路径为:/usr/local/zabbix/scripts/ # -* ...

  7. 将打开的网页以html格式下载到本地

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Python学习杂记_11_函数(二)

    函数的高级运用这部分知识的核心内容就是你要把“函数即变量”这个概念理解并运用得出神入化... 一.函数的递归调用 所谓递归调用就是函数自己调用自己,在Python中如果不做限制递归调用的死循环最多可循 ...

  9. 使用dom4j工具包对xml文件解析

    xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http: ...

  10. PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置+如何关闭快捷键ctrl+alt+方向键旋转屏幕

    如果映射的是eclipse的快捷键,又同时安装了英特尔的GPU软件,那么会有这个快捷键冲突 就是快速复制快捷键:ctrl+alt+方向键  会调用英特尔旋转屏幕,禁用掉即可 PhPStorm 是 Je ...