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.
- MVC、MVP以及Model2[下篇]
- Dora.Interception: 一个为.NET Core度身定制的AOP框架
- 为了支持AOP的编程模式,我为.NET Core写了一个轻量级的Interception框架[开源]
- 关于Expression Tree和IL Emit的所谓的"性能差别"
- 采用一个自创的"验证框架"实现对数据实体的验证[扩展篇]
- 采用一个自创的"验证框架"实现对数据实体的验证[改进篇]
- Flash XSS检测脚本的简单实现
- 采用一个自创的"验证框架"实现对数据实体的验证[设计篇]
- 采用一个自创的"验证框架"实现对数据实体的验证[编程篇]
- 谈谈你最熟悉的System.DateTime[上篇]
- 12步轻松搞定Python装饰器
- 实用小工具,教你轻松转化Python通用数据格式
- 数据工程师常用的几个小工具(附python源代码)
- R语言的三种聚类方法
- 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 数组属性和方法