### ngIf > ** 如果你希望根据一个条件来决定显示或隐藏一个元素,可以使用 ngIf 指令。这个条件是由你传给指令的表达式的结果决定的。
** 如果表达式的结果返回的是一个假值,那么元素会从DOM上被移除。 ```html # 例子:
``` ### ngSwitch > ** 实际作用同等于 switch语句 ```html # 通过2组实例进行比较 ## ngIf 根据同值不同条件渲染不同元素
Var is A
Var is B
Var is something else
增加条件与值
Var is A
Var is B
Var is C
Var is something else
### ngSwitch 改变上述情况,官方加入 ngSwitch 使用 ngSwitchCase 指令描述已知结果 使用 ngSwitchDefault 指令处理所有其他未知情况 重写实例
Var is A
Var is B
Var is something else
添加条件: ngSwitchDefault 元素是可选的。如果我们不用它,那么当 myVar 没有匹配到任何期望的值 时就不会渲染任何东西。
Var is A
Var is B
Var is C
Var is something else
为不同的元素声明同样的 *ngSwitchCase 值 在下面的例子中,当 choice 的值是 2 的时候,第2个和第5个 li 都会被渲染。

Current choice is {{ choice }}

``` ### ngStyle > 第一种用法:使用 ngStyle 指令,可以通过Angular表达式给特定的DOM元素设定CSS属性。 该指令最简单的用法就是 [style.]="value" 的形式, ```html
Uses fixed yellow background
``` > 第二种用法:设置固定值的方式就是使用 ngStyle 属性,使用键值对来设置每个属性 ```html
Uses fixed white text on blue background
``` > 第三种用法:这个才是ngStyle的出彩的地方 ```html
{{ color }} text
{{ color }} background
``` ```typescript // 定义数据类型,定义触发事件 export class InventoryAppComponent implements OnInit { color:string; fontSize:number; apply(color: string, fontSize: number) { this.color = color; this.fontSize = fontSize; } constructor() { } ngOnInit() { } } ``` ### ngClass > ngClass 指令在HTML模板中用 ngClass 属性来表示,让你能动态设置和改变一个给定DOM元素的CSS类。 以下是最简单的用法。 ```scss .bordered { border: 1px dashed black; background-color: #eee; } ``` ```html
This is never bordered
This is always bordered
``` > 动态添加class ```html
Using object var. Border `{{ classesObj.bordered ? "ON" : "OFF" }}`

Using object literal. Border `{{ isBordered ? "ON" : "OFF" }}`

This will always have a blue background and round corners
This is { { classList.indexOf('blue') > -1 ? "" : "NOT"}} blue and { { classList.indexOf('round') > 1 ? "" :"NOT" }} round
``` ```typescript // 定义数据,并赋值 export class InventoryAppComponent implements OnInit { isBordered: boolean; classesObj: Object; classList: string[]; constructor() { this.isBordered = true; this.classesObj = { bordered : "bordered" }; this.classList = ['blue', 'round']; console.log(this) } ngOnInit() { } } ``` ### ngFor > 这个指令的任务是重复一个给定的DOM元素 (或一组DOM元素),每次重复都会从数组中取 一个不同的值。
它的语法是 *ngFor="let item of items" 。 `简化版` ```typescript cities: string[]; constructor() { this.cities = ['Miami', 'Sao Paulo', 'New York']; } ``` ```HTML

Simple list of strings

{{ c }}
Miami Sao Paulo New York ``` `进阶版` ```typescript people: Object[]; constructor() { this.people = [ { name: 'Anderson', age: 35, city: 'Sao Paulo' }, { name: 'John', age: 12, city: 'Miami' }, { name: 'Peter', age: 22, city: 'New York' } ]; } ``` ```HTML

table

List of objects

Name Age City
{{ p.name }} {{ p.age }} {{ p.city }}
``` `输出` Name|Age|City :----:|:----:|:----: Anderson|35|Sao Paulo John|12|Miami Peter|22|New York `嵌套数组` ```HTML

Nested data

{{ item.city }}

Name Age
{{ p.name }} {{ p.age }}
``` ```typescript peopleByCity: Object; constructor() { this.peopleByCity = [ { city: 'Miami', people: [ { name: 'John', age: 12 }, { name: 'Angel', age: 22 } ] }, { city: 'Sao Paulo', people: [ { name: 'Anderson', age: 35 }, { name: 'Felipe', age: 36 } ] } ]; } ``` `输出 Miami` Name|Age :-:|:-: John|12 Angel|22 `输出 Sao Paulo` Name|Age :-:|:-: Anderson|35 Felipe|36 `获取索引` ```html
{{ num+1 }} - {{ c }}
1-Miami 2-Sao Paulo 3-New York ``` ### ngNonBindable > 当我们想告诉Angular不要编译或者绑定页面中的某个特殊部分时,要使用 ngNodBindable指令。 ```html
{{content}} ←This is what {{ content }} rendered
输出 文章 ←This is what {{ content }} rendered ```