angularjs - AngularJS 1.5中的组件和指令之间的主要区别是什么?

标签 angularjs angular-directive

我发现很难理解码件和指令之间的真正区别。我现在知道,组件是一个容易得多的概念。

因此,考虑到这一点,在使用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/

相关文章:

javascript - .when 可以用 Angular $routeProvider 动态生成吗?

javascript - 当绑定(bind)项是对象并动态修改时,单向绑定(bind)不起作用

html - 如何制作结构指令来包装我的 DOM 的一部分?

angularjs - 通过 ng-repeat 渲染 Angular 嵌套指令

javascript - Angular.js : custom directive

javascript - 在 Angular 分量中使用 'require'

javascript - 如何在 Angular 应用程序中隐藏损坏的图像

javascript - .run 为所有 View 触发 - AngularJS

javascript - Angular Directive 未运行且 templateUrl 未显示

javascript - 在 Angular 2 中以编程方式加载和卸载模块