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. TOPCoder(一)Time

         Class: Time Method: whatTime Parameters: int Returns: String Method signature: String whatTime( ...

  2. CSU 2031

    2031: Barareh on Fire Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitt ...

  3. idea创建maven项目需要注意的问题

    idea创建maven项目之后,我从deployment中看到报部署错误的问题,下图是解决问题的办法如下图所示:

  4. MAC中如何配置两套android-sdk环境

    1 背景介绍 随着android studio的完善和普及,越来越多Android app开发者从Eclipse+ADT环境转到Android Studio(后文统一用AS表示).但是,AS往往需要较 ...

  5. IOS深入学习(21)之Key-value coding

    http://blog.csdn.net/developer_zhang/article/details/12840567 1 前言 本节我们主要介绍键值编码,以及如何使一个类符合KVC编码. 2 详 ...

  6. 初学springMVC的拦截器

    springMvc拦截器的执行顺序! 拦截器的各个方法的作用: /** * 登录验证拦截器 */      public class Intercepter implements HandlerInt ...

  7. Ajax不能跨域访问的解决方案

      文章介绍 这是一篇,引导文吧... 因为写这篇文章时,实在想不出该如何分序.因此以实现跨域访问为目的,从基础知识往上写.最后以百度搜索智能提示为例,来讲解跨域的具体应用! 内容 首先,我们得明确什 ...

  8. js-禁止微信H5页面点击右上角菜单时出现“复制链接”,且分享仅支持微信分享

    禁止微信H5页面点击右上角菜单时出现“复制链接”,这个问题已经影响到我很久很久了,起码有2年了, 昨天写H5活动的时候,需求有一个是:可分享,但是禁止复制活动链接, 这一下,就逼我务必好好研究研究了. ...

  9. (1)php开篇常识

    一.php PHP由zend公司开发维护,目前最高版本PHP7.1,官网地址http://php.net/ PHP从5.5开始不支持XP,没有PHP6这个版本 PHP是嵌入到HTML的脚本代码,格式: ...

  10. hdu3001(状态压缩DP)

    hdu3001 题意 选择从任意一点出发,经过所有点的最小花费(经过每个点的次数不能多于 2 次). 分析 类似于 poj3311 经过每个点的次数有限制,考虑用三进制数存储每个点被访问过的次数,其它 ...