Angular built-in指令分为attribute指令和structural指令两种。

Attribute指令

最常用的attribute指令有NgClass, NgStyle和NgModel三种。

NgClass

动态添加或者删除html标签的css类。

例子:

<!-- toggle the "special" class on/off with a property -->
<div [ngClass]="isSpecial ? 'special' : ''">This div is special</div>

isSpecial为true时,div标签打上special的css类。

一个更复杂一些的例子:

<div [ngClass]="currentClasses">This div is initially saveable, unchanged, and special.</div>

div的class绑定到了Component的property,名为currentClasses.

在Component的实现代码里,currentclasses的赋值逻辑:

currentClasses: {};
/* . . . */setCurrentClasses() {// CSS classes: added/removed per current state of component propertiesthis.currentClasses =  {saveable: this.canSave,modified: !this.isUnchanged,special:  this.isSpecial};}

NgModel

例子:

<label for="example-ngModel">[(ngModel)]:</label>
<input [(ngModel)]="currentItem.name" id="example-ngModel">

上面实际上是一个双向绑定的语法糖,等价于:

<label for="without">without NgModel:</label>
<input [value]="currentItem.name" (input)="currentItem.name=$event.target.value" id="without">

Structural指令

ngIf

例子:

<div *ngIf="hero" class="name">{{hero.name}}</div>

实际是个语法糖,等价于:

<ng-template [ngIf]="hero"><div class="name">{{hero.name}}</div>
</ng-template>

ngFor

例子:

<app-item-detail *ngFor="let item of items" [item]="item"></app-item-detail>

let item of items的含义:Take each item in the items array, store it in the local item looping variable, and make it available to the templated HTML for each iteration.

ngFor指令有一个内置属性index:

<div *ngFor="let item of items; let i=index">{{i + 1}} - {{item.name}}</div>

上面的例子,将index属性赋给template变量i.

Angular的built-in指令相关推荐

  1. Angular如何自定义attribute指令

    需求: 实现一个自定义的attribute directive,当施加到某个html element时,鼠标hover上去,会修改其背景颜色. Highlight me! 下面是具体做法. (1) 使 ...

  2. Angular应用的路由指令RouterLink

    Angular应用的路由指令RouterLinkapp Component html源代码: <h1>{{title}}</h1> <nav><a route ...

  3. Angular深入理解之指令

    Angular深入理解之指令 指令有什么功能 Attribute directives 属性指令 Structural directives 结构指令 自定义属性指令 自定义结构指令 Angular深 ...

  4. Angular的内置指令

    Angular的内置指令 Angular的三种指令包括 渲染指令 事件指令 节点指令 渲染指令 ng-init:初始化.初始化操作,这里用一段代码解释一下吧,代码如下: <div ng-init ...

  5. 浅谈Angular如何自定义创建指令@Directive

    ​知识普及 Angular 指令根据其创建形式分为内置指令和自定义指令,指令按照类型分: 模板指令--组件就是模板指令(只能自定义) 属性型指令 -- 更改元素.组件或其他指令的外观或行为的指令(有内 ...

  6. Hacking with Angular: 玩转ngOptions指令

    首先说说为什么要详细的了解一下这个指令,因为在工作中总是遇到关于下拉选项的一些操作,但是又常常会出现一些问题:基本会遇到下面一些问题: 关于下拉框使用ng-repeat或者ng-options指令的区 ...

  7. Angular 一个简单的指令实现 阻止事件扩散

    //指令定义 @Directive({selector: `click-stop-propagation`events: 'stopClick($event)' }) class ClickStopP ...

  8. Angular里的RouterOutlet指令学习笔记

    官网:https://angular.io/api/router/RouterOutlet#description Acts as a placeholder that Angular dynamic ...

  9. angular与angularjs常用指令的不同写法整理

    angularjs与angular 常用的指令写法的区别; 一:angularjs指令 1.ng-bind 使用给定的变量或表达式的值来替换 HTML 元素的内容 <p ng-bind=&quo ...

  10. Angular 自定义结构化指令,如何传入多个输入参数

    如下图所示,自定义指令 cxPopover,包含了两个输入参数: 消费的 html 页面: 看起来,cxPopOver 和 cxPopoverOptions 是两个不同的指令,然而其实际上是定义在同一 ...

最新文章

  1. 将AI落地到福州、贵阳的「幕后推手」,到底在想些什么?
  2. 把鼠标、触摸屏、触控笔统一起来,Pointer Events介绍
  3. Visual Studio 2010中C++项目升级指南
  4. 博士论文:教你如何给女朋友送礼物
  5. SAP MM/FI 自动过账实现 OBYC 接口执行
  6. ahjesus Axure RP 7.0注册码
  7. 树叶节点_茶,一片树叶的旅程
  8. 机器人吴非_Nature封面:8天做完688个实验,每天工作21.5小时,实验机器人展现超强战斗力...
  9. 【Elasticsearch】Elasticsearch 存储桶聚合
  10. 教程-TObjectList.Clear、TStringList.Clear方法对象有没有被释放
  11. 众说纷纭的ul、ol、li
  12. 《软件质量保证与测试》学习笔记【第一章 软件测试基本概念】
  13. 智慧城管统计考评详解
  14. 免费下载3小时学会Excel数据处理视频教程
  15. 关于主机名,域名区别(www.baidu.com与baidu.com)以及同源策略的一些误区
  16. 【万恶之源~10大经典表情包出处】
  17. 2011年6个微博营销趋势
  18. 赚钱的地方,除了工资,还有这些
  19. c语言:购房从银行贷了一笔款d,准备每月还款额为p,月利率为r,计算多少月能还清。...
  20. c语言百万富翁换钱的程序,百万富翁的换钱计划.docx

热门文章

  1. 读书笔记:软件人才-管理的艺术
  2. 反写规则-销售订单关闭后不允许出库 (销售订单-销售出库单)
  3. 微信开发同步微信服务器图片到本地,逻辑处理。
  4. Tsinsen A1493 城市规划(DP + CDQ分治 + NTT)
  5. JSP的改动需要重启应用服务器才能生效?
  6. 针对sqoop1.99.6 从jdbc向hdfs中抽取数据的几点感想
  7. jQuery-1.9.1源码分析系列(五) 回调对象
  8. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
  9. 发现一个增长天翼手机待机时间的方法
  10. 机器学习面试题(part4)