Angular 1.5组件与旧指令 - 哪里是链接功能? [英] Angular 1.5 component vs. old directive - where is a link function?

查看:208
本文介绍了Angular 1.5组件与旧指令 - 哪里是链接功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读最近这个很好的文章关于Angular 1.5中新的 .component()帮助器,它应该可以帮助每个人最终迁移到Angular 2。一切看起来都很简单,但我找不到任何有关组件内DOM操作的信息。

I've been reading this nice recent article about new .component() helper in Angular 1.5, which is supposed to help everyone to migrate to Angular 2 eventually. Everything looks nice and simple, but I couldn't find any information about DOM manipulation inside components.

有一个模板属性虽然,可以是一个函数并接受 $ element $ attrs 参数。我还不清楚这是否取代了链接功能。它似乎不是这样。

There is a template property though, which can be a function and accept $element and $attrs arguments. Still it's not clear to me if that's the replacement for a link function. It doesn't seem so.

推荐答案

编辑2/2/16: 1.5文档现在涵盖组件: https://docs.angularjs.org/guide/component

EDIT 2/2/16: The 1.5 documentation now covers components: https://docs.angularjs.org/guide/component

基于某些阅读的一些想法(以下链接):

Some thoughts based on some reading (links below):


  • 组件不是指令的替代品。组件是一种特殊类型的指令,用于组织带有模板的控制器。

  • Components aren't replacements for directives. A component is a special type of directive that organizes a controller with a template.

组件没有链接功能,控制器仍然不在你的位置处理DOM操作。

Components do not have a link function and controllers still are not where you'd handle DOM manipulation.

如果需要DOM操作,组件可以在链接函数中使用包含DOM操作的其他指令。

If you need DOM manipulation, your component can use other directives that include that DOM manipulation in a link function.

我花了一段时间来弄明白这一点,但是一旦我做了它就有了一些意义:组件是指令但不是所有的指令都是 - 或者需要 - 组件。

It took me a while to figure this out, but once I did it made some sense: components are directives but not all directives are--or need to be--components.

当我认为组件正在替换指令时,关于链接函数的问题是自然的,或者对我来说。为什么?因为我们已经被教导将DOM操作放在指令的链接函数中:想要修改DOM的指令通常使用链接选项来注册DOM侦听器以及更新DOM。 https://docs.angularjs.org/guide/directive

The question about link functions is a natural one, or was to me, when I thought components were replacing directives. Why? Because we've been taught to put DOM manipulation inside a directive's link function: "Directives that want to modify the DOM typically use the link option to register DOM listeners as well as update the DOM." https://docs.angularjs.org/guide/directive.

如果你正在运行这个假设(组件替换指令),那么你会发现Angular文档没有回答这个问题,因为一旦你知道它就不是正确的问题组件的目的。 (组件在 $ compileProvider文档中描述,而不是指令文档。)

If you're running with that assumption (components replace directives), then you'll find that the Angular docs don't answer the question because, well, it's not the right question once you know the purpose of a component. (Components are described in the $compileProvider documentation not the directive documentation.)

我上面所说的实际上是对Todd Motto在组件和指令中可能是最好的讨论(到目前为止)所说的内容的改述:

What I say above is really a rephrasing of what Todd Motto has said in what's probably the best discussion (so far) on components and directives:

https://www.reddit.com/r / angularjs / comments / 3taxjq / angular_15_is_set_to_introduce_the_component /

将这些评论纳入更一般的文章可能会有用。

It could be useful to have those comments pulled out into a more general article.

关于组件的大多数文章都没有提到链接功能(这并不意味着这些不是优秀的文章):

Most articles on components don't mention a link function (this doesn't mean these aren't excellent articles):

< a href = https://toddmotto.com/exploring-the-angular-1-5-component-method/\"rel =nofollow noreferrer> https://toddmotto.com/exploring-the-angular-1-5-component-方法/

https://medium.com/@tomastrajan/component-paradigm-cf32e94ba78b#.vrbo1xso0

https://www.airpair.com/angularjs/posts/component-based-angularjs-directives

或者当提到链接功能时,它在括号中:

Or when the link function is mentioned it is in parentheses:

http://teropa.info/blog/2015/10/18/refactoring- angular-apps-to-components.html

一篇文章说组件使用控制器而不是链接功能。但这不是反而的情况:控制器不是链接功能的替身。

One article says that components, "use controllers instead of link functions." But it's not an "instead" situation: controllers aren't stand-ins for link functions.

这篇关于Angular 1.5组件与旧指令 - 哪里是链接功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆