angular4笔记系列之内置指令小结
网络编程 2021-07-04 16:47www.168986.cn编程入门
这篇文章主要介绍了angular4笔记系列之内置指令小结,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
内置指令
内置属性型指令
属性型指令会监听和修改其它HTML元素或组件的行为、元素属性(Attribute)、DOM属性(Property)。
NgClass
形式:[ngClass]="statement"
通过绑定到NgClass,可以同时添加或移除多个类。
setCurrentClasses() { this.currentClasses = { 'saveable': this.canSave, 'modified': !this.isUnchanged, 'special': this.isSpecial }; } <div [ngClass]="currentClasses">This div</div>
NgStyle
形式:[ngStyle]="statement"
NgStyle绑定可以同时设置多个内联样式。
setCurrentStyles() { this.currentStyles = { 'font-style': this.canSave ? 'italic' : 'normal', 'font-weight': !this.isUnchanged ? 'bold' : 'normal', 'font-size': this.isSpecial ? '24px' : '12px' }; } <div [ngStyle]="currentStyles">This div</div>
NgModel
形式:[(ngModel)]="statement"
使用[(ngModel)]双向绑定到表单元素。
<input [(ngModel)]="currentHero.name">
使用 ngModel 时需要 FormsModule
内置结构型指令
NgIf
形式:*ngIf="statement"
<app-hero-detail *ngIf="isActive"></app-hero-detail>
NgFor
形式:*ngFor="statement"
<div *ngFor="let hero of heroes">{{hero.name}}</div>
NgSwitch
形式:[ngSwitch]="statement"
<div [ngSwitch]="currentHero.emotion"> <app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> <app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> <app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> </div>
NgSwitch实际上包括三个相互协作的指令:NgSwitch、NgSwitchCase 和 NgSwitchDefault
模板引用变量 ( #var )
模板引用变量通常用来引用模板中的某个DOM元素,它还可以引用Angular组件或指令或Web Component。
<input #phone placeholder="phone number"> <button (click)="callPhone(phone.value)">Call</button>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程