angularjs component

2016-11-10 14:02:52 +08:00
 oliver34

angularjs 是不是不推荐 directive 了,我看官方 demo 都不用 directive 了,用 compontent ,两者有什么区别?

2326 次点击
所在节点    JavaScript
5 条回复
xingo
2016-11-10 22:29:22 +08:00
component 应该是以前 controller 的地位了吧, directive 还是有的, component 应该还是低级版的 directive
beginor
2016-11-11 06:16:38 +08:00
应该说 component 是简化版的 directive ,但是具备了生命周期的概念
hantsy
2016-11-11 09:51:36 +08:00
Angular 1.5 就组件化了, 每个组件有独立的 Scope ,开发更简单。概念上接近 Angular2 ( Component 同样的 lifecycle hook ,$onInit, $onchanges, $onDestroy, etc ), 升级到 Angular2 更容易 。

https://www.v2ex.com/t/310681#reply0
hantsy
2016-11-11 09:55:38 +08:00
在 Angular 2 中,@Component 本身也是简化的 Directive ,操作的是逻辑上的视图,而 @Directive 意味更多操作 DOM Attribute (如 NgClass, NgStyle ),或者修改 DOM Structure(如 NgFor, NgIf)。
lijsh
2016-11-12 23:52:10 +08:00
可以看看这里 http://cn.vuejs.org/v2/guide/comparison.html#指令与组件

Angular 1 早期的版本并没有 component 的 API ,组件和自定义 DOM 操作都使用指令实现; v1.5 新增了 component 的概念, component 的 API 比指令更简单,不涉及到 DOM 操作,只允许以自定义元素的形式指定。

这实际上就是把以前指令中有关组件的实现拆分出来了。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/319536

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX