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

查看:20
本文介绍了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.

虽然有一个 template 属性,它可以是一个函数并接受 $element$attrs 参数.我仍然不清楚这是否是 link 函数的替代品.好像不是.

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.

推荐答案

EDIT 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/评论/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):

https://toddmotto.com/exploring-the-angular-1-5-component-method/

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天全站免登陆