经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » AngularJS » 查看文章
angular4笔记系列之内置指令小结
来源:jb51  时间:2018/11/9 17:16:41  对本文有异议

内置指令

内置属性型指令

属性型指令会监听和修改其它HTML元素或组件的行为、元素属性(Attribute)、DOM属性(Property)。

NgClass

形式:[ngClass]="statement"

通过绑定到NgClass,可以同时添加或移除多个类。

  1. setCurrentClasses() {
  2. this.currentClasses = {
  3. 'saveable': this.canSave,
  4. 'modified': !this.isUnchanged,
  5. 'special': this.isSpecial
  6. };
  7. }
  8.  
  9. <div [ngClass]="currentClasses">This div</div>

NgStyle

形式:[ngStyle]="statement"

NgStyle绑定可以同时设置多个内联样式。

  1. setCurrentStyles() {
  2. this.currentStyles = {
  3. 'font-style': this.canSave ? 'italic' : 'normal',
  4. 'font-weight': !this.isUnchanged ? 'bold' : 'normal',
  5. 'font-size': this.isSpecial ? '24px' : '12px'
  6. };
  7. }
  8.  
  9. <div [ngStyle]="currentStyles">This div</div>
  10.  

NgModel

形式:[(ngModel)]="statement"

使用[(ngModel)]双向绑定到表单元素。

  1. <input [(ngModel)]="currentHero.name">

使用 ngModel 时需要 FormsModule

内置结构型指令

NgIf

形式:*ngIf="statement"

  1. <app-hero-detail *ngIf="isActive"></app-hero-detail>

NgFor

形式:*ngFor="statement"

  1. <div *ngFor="let hero of heroes">{{hero.name}}</div>

NgSwitch

形式:[ngSwitch]="statement"

  1. <div [ngSwitch]="currentHero.emotion">
  2. <app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero>
  3. <app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero>
  4. <app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero>
  5. </div>

NgSwitch实际上包括三个相互协作的指令:NgSwitch、NgSwitchCase 和 NgSwitchDefault

模板引用变量 ( #var )

模板引用变量通常用来引用模板中的某个DOM元素,它还可以引用Angular组件或指令或Web Component。

  1. <input #phone placeholder="phone number">
  2.  
  3. <button (click)="callPhone(phone.value)">Call</button>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号