为什么指令有范围有多大? [英] Why directive has scope?

查看:116
本文介绍了为什么指令有范围有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,基本上范围是控制器的一个实例。

As far as I know that basically scope is an instance of a controller.

我每次申报控制器范围将用于该控制器。

Every time I declare a controller scope will be available for that controller.

不过,为什么指令有范围在链接功能?

But then why directive has scope in link function ?

我没有申报的指令,任何控制器。

I didn't declare any controller for the directive.

那么,为什么链接函数范围?任何想法?

Then why link function has scope ? Any Idea ?

推荐答案

DOC

适用范围:
  要由该指令用于登记手表的使用范围

您可能也有兴趣看看之间的差异$范围和范围

所有的指令都与他们有关的范围。他们利用这个范围来访问模板和链接功能的内部数据/方法。默认情况下,除非明确设置,指令不会创建自己的范围。因此,指令使用他们的父范围(通常为控制器)作为自己的。

All directives have a scope associated with them. They use this scope for accessing data/methods inside the template and link function. By default, unless explicitly set, directives don’t create their own scope. Therefore, directives use their parent scope ( usually a controller ) as their own.

不过,AngularJS让我们通过传递被称为指令定义对象的配置对象来更改指令的默认范围。一个指令定义对象 - 让我们称其为DDO - 用于配置该指令的行为,template..etc一个简单的JavaScript对象。退房AngularJS 文档约DDO。

However, AngularJS allows us to change the default scope of directives by passing a configuration object known as directive definition object. A directive definition object –– let’s call it as DDO –– is a simple JavaScript object used for configuring the directive’s behaviour,template..etc. Check out AngularJS docs about DDO.

所以,我们可以使用的范围内联函数父作用域,子作用域或孤立范围工作。

So, we can use scope inside linking function to work with parent scope, child scope or isolated scope.

您可以访问的有关内部指令范围的详细信息。

You may visit this for detailed information about scope inside directive.

您可以共享控制器和链接功能之间的数据。

有时我们想访问角指令在两个控制器和链路功能相同的数据。通常我们这个数据添加到的范围对象。这具有一定的问题,例如将其暴露于任何子范围。共享的属性添加到控制器实例本身和访问的链接功能。

Sometimes we would like to access that same data in both controller and link functions in Angular directive. Usually we add this data to the scope object. This has certain problems, for example exposing it to any child scope. Add the shared properties to the controller instance itself and access in the link function.

您还可能有兴趣在<一个href=\"http://stackoverflow.com/questions/20018507/angularjs-what-is-the-need-of-the-directives-link-function-when-we-already-had\">what是需要链接功能?

这篇关于为什么指令有范围有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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