Angular提供了一些内置指令(directive),这些添加在HTML元素(element)上的特性(attribute)可以赋予更多的动态行为。

NgIf

ngIf指令用于在某个条件下显示或者隐藏元素,该条件取决于传入指令的表达式的结果。

<div *ngIf="false"></div> <!-- 永远不显示 -->
<div *ngIf="a > b"></div> <!-- a大于b时显示 -->
<div *ngIf="str == 'yes'"></div> <!-- str等于"yes"时显示 -->
<div *ngIf="myFunc()"></div> <!-- myFunc返回true时显示 -->

NgSwitch

ngSwitch指令用于处理更复杂的显示逻辑。

<div class="container" [ngSwitch]="myVar">
<div *ngSwitchCase="'A'">Var is A</div>
<div *ngSwitchCase="'B'">Var is B</div>
<div *ngSwitchCase="'C'">Var is C</div>
<div *ngSwitchDefault>Var is something else</div>
</div>

ngSwitchDefault为可选项,如果不加上它,且找不到匹配项时,没有内容将在页面上渲染出来。

NgStyle

ngStyle指令最简洁的方式是[style.<cssproperty>]="value"这样的形式。

<div [style.background-color]="'yellow'">
Uses fixed yellow background
</div>

另一种方法,采用了键值组合的形式:

<div [ngStyle]="{color: 'white', 'background-color': 'blue'}">
Uses fixed white text on blue background
</div>

NgClass

ngClass指令也是一种设置样式的方式,同样也有两种设置办法。

第一种是传递对象字面量(object literal),对象的键是样式的类名,值为布尔类型,指明是否应用样式类。

.bordered {
border: 1px dashed black;
background-color: #eee;
}
<div [ngClass]="{bordered: false}">This is never bordered</div>
<div [ngClass]="{bordered: true}">This is always bordered</div>

另一种是直接指定样式类名。

<div class="base" [ngClass]="['blue', 'round']">
This will always have a blue background and
round corners
</div>

ngClass中添加的样式类会与HTML的class特性中已有样式合并,成为最终的'class'结果。

NgFor

ngFor指令用于迭代集合项,其语法为 *ngFor="let item of items"

let item语法指定每次迭代的元素,items是集合项。

ngFor指令中的集合项可以是对象的数组。

this.people = [
{ name: 'Anderson', age: 35, city: 'Sao Paulo' },
{ name: 'John', age: 12, city: 'Miami' },
{ name: 'Peter', age: 22, city: 'New York' }
];

同时还支持嵌套:

<h4 class="ui horizontal divider header">
Nested data
</h4> <div *ngFor="let item of peopleByCity">
<h2 class="ui header">{{ item.city }}</h2> <table class="ui celled table">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tr *ngFor="let p of item.people">
<td>{{ p.name }}</td>
<td>{{ p.age }}</td>
</tr>
</table>
</div>

在要获得索引的场合下,只需要稍加变化。

<div class="ui list" *ngFor="let c of cities; let num = index">
<div class="item">{{ num+1 }} - {{ c }}</div>
</div>

NgNonBindable

ngNonBindable指令负责通知Angular,页面上的某块区域不用额外编译。

<div class='ngNonBindableDemo'>
<span class="bordered">{{ content }}</span>
<span class="pre" ngNonBindable>
&larr; This is what {{ content }} rendered
</span>
</div>

上述例子中,第二个span区块里的内容不会被Angualr编译。

ng-book札记——内置指令的更多相关文章

  1. Angular内置指令(一)

    要注意的是不要把自己开发的指令以ng开头,以免与内置指令冲突  目录:ng-disabled,ng-readonly,ng-checked,ng-selected,ng-href,ng-src,ng- ...

  2. AngularJS内置指令

    指令,我将其理解为AngularJS操作HTML element的一种途径. 由于学习AngularJS的第一步就是写内置指令ng-app以指出该节点是应用的根节点,所以指令早已不陌生. 这篇日志简单 ...

  3. angularJS内置指令一览

    基础ng指令 ng-href ng-src ng-disabled ng-readonly ng-checked ng-selected ng-class ng-style ng-show ng-hi ...

  4. Angular内置指令

    记录一下工作中使用到的一些AngularJS内置指令 内置指令:所有的内置指令的前缀都为ng,不建议自定义指令使用该前缀,以免冲突 1. ng-model 使用ng-model实现双向绑定,通过表单的 ...

  5. AngularJS学习笔记(四)内置指令

    说说指令 不得不赞叹,指令是ng最为强大的功能之一,好吧,也可以去掉之一,是最强大的功能.ng内置了许多自定义的指令,这避免了我们自己去造轮子.同时,ng也提供了自定义指令的功能,可以让我们的页面元素 ...

  6. AngularJs -- 内置指令

    AngularJS提供了一系列内置指令.其中一些指令重载了原生的HTML元素,比如<form>和<a>标签, 当在HTML中使用标签时,并不一定能明确看出是否在使用指令. 其他 ...

  7. Angular中的内置指令和自定义指令

    NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...

  8. angular内置指令相关知识

    原文地址 https://www.jianshu.com/p/5a5b43a8e91f 大纲 1.angular指令的分类 2.angular指令之——组件 3.angular指令之——属性指令 (n ...

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

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

随机推荐

  1. python--同步锁/递归锁/协程

    同步锁/递归锁/协程 1 同步锁 锁通常被用来实现对共享资源的同步访问,为每一个共享资源创建一个Lock对象,当你需需要访问该资源时,调用acquire()方法来获取锁对象(如果其他线程已经获得了该锁 ...

  2. python判断素数的方法

    #运用python的数学函数 import math def isPrime(n): if n <= 1: return False for i in range(2, int(math.sqr ...

  3. 在python后台如何将客户端提交的form表单数据提取出来?

    1.获取客户端提交的表达数据,数据类型为ImmutableMultiDictformData = request.form2.将提取的数据转化成字典formDict = formData.to_dic ...

  4. Django REST framework+Vue 打造生鲜超市(十二)

    十三.首页.商品数量.缓存和限速功能开发  13.1.轮播图接口实现 首先把pycharm环境改成本地的,vue中local_host也改成本地 (1)goods/serializer class B ...

  5. [ABP]浅谈模块系统与 ABP 框架初始化

    在 ABP 框架当中所有库以及项目都是以模块的形式存在,所有模块都是继承自AbpModule 这个抽象基类,每个模块都拥有四个生命周期.分别是: PreInitialze(); Initialize( ...

  6. Leetcode 804. Unique Morse Code Words 莫尔斯电码重复问题

    参考:https://blog.csdn.net/yuweiming70/article/details/79684433 题目描述: International Morse Code defines ...

  7. [LeetCode] Range Module 范围模块

    A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...

  8. 原生http请求封装

    满血复活,今天开始开始更新博客.随着es6的普遍应用,promise属性也随之用之普遍,我们在一些项目中,为了避免引入一些http库,节省空间,就简单将原生http请求做了封装处理,封装代码如下:(其 ...

  9. Django框架之虚拟环境搭建

    创建虚拟环境篇 今天小编就来讲一下在Ubantu下如何搭建Django环境,希望能帮助那些不会搭建的童鞋^o^ 0.首先要先安装好Python环境,至于安装过程,小编就不讲了,百度一下,你懂得.. 1 ...

  10. ●CodeForces 549F Yura and Developers

    题链: http://codeforces.com/problemset/problem/549/F题解: 分治,链表. 考虑对于一个区间[L,R],其最大值在p位置, 那么答案的贡献就可以分为3部分 ...