常用指令
NgClass#
-
作用:添加或移除一组 CSS 类
NgStyle#
-
作用:添加或移除一组 CSS 样式
NgModel#
-
作用:双向绑定到 HTML 表单元素
NgIf#
-
作用:根据条件添加或移除 DOM
-
语法:
<div class="box" *ngIf="false">看不见我</div>
<!-- isSpecial is true --> <div [class.hidden]="!isSpecial">Show with class</div> <div [class.hidden]="isSpecial">Hide with class</div> <div [style.display]="isSpecial ? 'block' : 'none'">Show with style</div> <div [style.display]="isSpecial ? 'none' : 'block'">Hide with style</div>
#
-
作用:类似于 JavaScript 中的 switch 语句,根据条件渲染多个选项中的一个。
-
语法:该指令包括三个相互协作的指令:
NgSwitch
、NgSwitchCase
、NgSwitchDefault
<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-confused-hero *ngSwitchCase="'confused'" [hero]="currentHero"></app-confused-hero> <app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> </div>
NgFor#
-
作用:列表渲染
-
语法:
<div *ngFor="let hero of heroes">{{hero.name}}</div>
#
<ul> <li *ngFor="let item of todos; let i = index">{{ item.title + i }}</li> </ul>
#
参考文档:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2020-03-19 数据库读写分离之配置Django实现数据库读写分离