Angular的built-in指令
时间:2022-07-23
本文章向大家介绍Angular的built-in指令,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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 properties
this.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.
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 实战丨云开发帮你和「火箭少女」合个影!
- 开源数据闪回工具—binlog2sql介绍
- 盘点前端面试常见的15个TS问题,你能答对吗?
- 『深度思考』对CenterNet的一些思考与质疑·测试对比CenterNet与U版YoloV3速度与精度
- 优秀员工应该具备的11个特质
- 腾讯位置服务教你快速实现距离测量小工具
- 一张PDF了解JDK11 GC调优秘籍-附PDF下载
- 如何用函数框架快速开发大型 Web 应用 | 实战
- 划重点 | Android Jetpack 三大重要更新!
- JVM系列之:详解java object对象在heap中的结构
- 微信会话语音文件的一句话识别
- 看动画学算法之:排序-归并排序
- 看动画学算法之:排序-选择排序
- 你可能不知道的9条Webpack优化策略
- SPI