[Angular] ngClass conditional
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的更多相关文章
- angular ng-class使用笔记
在前面Angularjs开发一些经验总结中说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJO( ...
- Angular - - ngClass、ngClassEven、ngClassOdd、ngStyle
这几个都关于样式及类名修改的,所以先把样式代码贴上吧. .red{color:red} .blue{color:blue} 写案例用到的样式就这么简单的两个,下面进入正题. ngClass ngCla ...
- angular [NgClass] [NgStyle],NgIf,[ngSwitch][ngSwitchCase]
[NgClass] CSS 类会根据表达式求值结果进行更新,更新逻辑取决于结果的类型: string - 会把列在字符串中的 CSS 类(空格分隔)添加进来, Array - 会把数组中的各个元素作 ...
- angular -- ng-class该如何使用?
ng-class是一个判断是否给某一个元素添加类名的属性: 例如:下面是判断 是否添加 aHover 这个类名: <ul class="nav fl w120 o"> ...
- AngularJS 的表单验证
最近开始学习angularjs,学到表单验证的时候发现有必要学习下大神的好文章: 转:http://www.oschina.net/translate/angularjs-form-validatio ...
- 一步一步弄懂angularJS基础
问题1:ng-app指令的使用以及自定义指令 <!doctype html> <!--这里的ng-app的属性值就是模块的名称,也就是 angular.module("My ...
- 浅谈Angularjs至Angular2后内置指令的变化
一.科普概要说明 我们常说的 Angular 1 是指 AngularJS: 从Angular 2 开始已经改名了.不再带有JS,只是单纯的 Angular: Angular 1.x 是基于JavaS ...
- angular学习之关于ng-class详解
1,定义和用法 ng-class 指令用于给 HTML 元素动态绑定一个或多个 CSS 类. ng-class 指令的值可以是字符串,对象,或一个数组. 如果是字符串,多个类名使用空格分隔. 如果是对 ...
- angular的ng-class
项目内想到要替换class时,第一反应是使用angular最为简单粗暴的class改变方式: 在angular中为我们提供了3种方案处理class: 1:scope变量绑定,如上例.(不 ...
随机推荐
- 《c程序设计语言》-2.6~2.8
#include <stdio.h> unsigned setbits(unsigned x, int p, int n, unsigned y) { return (x & (( ...
- Java面试题之类加载器有哪些?什么是双亲委派模型
类加载器有哪些: 1.启动类加载器(Bootstrap ClassLoader):这个类加载器负责将存放在<JAVA_HOME>\lib目录中的,或被-Xbootclasspath参数所指 ...
- 割点与桥,强连通分量,点双,边双[poj_1236]学校网络
割点与桥 题目描述 给定一张无向图G(V,E),你需要找出所有的割点与桥. 输入 第一行给出两个正整数V,E. 接下来E行每行两个正整数x,y,表示有一条连接x,y的边. 输出 输出共2行,第一行输出 ...
- 使用QML创建界面(转)
原文转自 https://blog.csdn.net/rl529014/article/details/51378307 在Qt编程中,我们可以使用纯C++代码,或C++和XML结合的方式来创建GUI ...
- wget下载整个网站的方法
转自: http://blog.itpub.net/29867/viewspace-716088/ (修改部分内容) wget --restrict-file-name=ascii -m -c -n ...
- c中结构体的4种定义
1.常规的标准方式: 1 #include <stdio.h> 2 3 struct student{ 4 int age; 5 float score; 6 ...
- super真的是调用父类吗?
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat class A: def __init__(self): print(&q ...
- python 向mysql插入数据
生成随机内容用到的方法: substr是一个字符串函数,从第二个参数1,开始取字符,取到3 + floor(rand() * 75)结束 floor函数代表的是去尾法取整数. rand()函数代表的是 ...
- js 抽奖小案例
Luck Draw 在线演示:九宫格抽奖 对九宫格形式的抽奖页面进行了一些简单封装.以后有机会再更新其他形式的抽奖.
- java中的BigInteger
头文件 import java.io.*; import java.math.*; 读入 Scanner cin = Scann(System.in); while(cin.hasNext()) &l ...