Angular2--Directives

Angular 模版(templates)是动态的. 当Angular 渲染模板时, 会根据directives里的说明来转化成DOM.

一个 directive 就是一个有 @Directive decorator 的class . 一个 component 是一个 directive + template; 一个 @Component decorator 实际上是一个有模板的 @Directive decorator。

While a component is technically a directive, components are so distinctive and central to Angular applications that this architectural overview separates components from directives.

从技术角度来讲,一个 component 就是 一个directive, components 在 Angular 应用中很重要,所以有必要将component和directive区分一下.

存在两种directives: 结构directives 和 属性directives.

通常directives会和属性一样出现在元素标签里,有时是通过name,但更多的是一个声明或绑定的target。

结构 directives 通过增、删、改DOM元素来改变布局。

<li *ngFor="let hero of heroes"></li>
<app-hero-detail *ngIf="selectedHero"></app-hero-detail>

*ngFor*ngIf 就是结构指令

属性 directives 改变一个已有元素的样式或行为。在模板中看起来很像普通的HTML 属性。

ngModel directive 声明来一个双向绑定,就是一个属性directive。 ngModel 编辑一个已有元素 (通常是一个 ) 的行为,通过设置它的属性和响应绑定事件。

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

Angular 还有一些可以改变布局的指令 (例如, ngSwitch) 或者修改DOM 元素或者 components (例如, ngStyle and ngClass).

当然你也可以自己写directives 。

angular.io/guide

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值