我发现很难理解码件和指令之间的真正区别。我现在知道,组件是一个容易得多的概念。
因此,考虑到这一点,在使用AngularJS 1.5时是否有任何理由继续使用指令?
也许我在这里没有真正的上下文,但是看来组件提供了更简单的API。
那我为什么要继续使用指令呢?
Angular 2指出一切都是组件,因此要努力从1.5轻松迁移到2,难道只使用以后的组件没有意义吗?
过去,我曾经使用伪指令创建例如自动查找文本框,没有理由为什么我现在不应该在组件内部执行此操作,对吗?然后,我可以在创建的其他组件中重复使用该组件吗?
最佳答案
只需复制 Angular 文档,因为他们以我认为的最佳方式进行了介绍。
Understanding Components
In Angular, a Component is a special kind of directive that uses a simpler configuration which is suitable for a component-based application structure.
This makes it easier to write an app in a way that's similar to using Web Components or using Angular 2's style of application architecture.
Advantages of Components:
- simpler configuration than plain directives
- promote sane defaults and best practices
- optimized for component-based architecture
- writing component directives will make it easier to upgrade to Angular 2
When not to use Components:
- for directives that rely on DOM manipulation, adding event listeners etc, because the compile and link functions are unavailable
- when you need advanced directive definition options like priority, terminal, multi-element
- when you want a directive that is triggered by an attribute or CSS class, rather than an element
更多阅读:https://docs.angularjs.org/guide/component
关于angularjs - AngularJS 1.5中的组件和指令之间的主要区别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38342664/