[英]Angular two-way binding vs method binding
Let's assume we got OuterDirective and InnerDirective. 假设我们有OuterDirective和InnerDirective。 InnerDirective is displaying temp which is provided by OuterDirective like:
InnerDirective正在显示由OuterDirective提供的温度,例如:
<outer-directive>
<inner-directive temp="temp" />
</outer-directive>
1) In inner directive we can set up two way binding: temp: '=' and in inner directive tpl use {{ temp }} 1)在内部指令中,我们可以设置两种方式绑定:temp:'=',在内部指令tpl中,使用{{temp}}
Or 要么
2) we can use method binding "&" like temp: '&' and in inner directive tpl {{ temp() }} 2)我们可以像temp:'&'那样在内部指令tpl {{temp()}}中使用方法绑定“&”
The temp is changing so we need to update inner directive. 温度在变化,因此我们需要更新内部指令。
Well, in the first case, temp
can be an arbitrary variable which is provided by the OuterDirective
's scope. 好吧,在第一种情况下,
temp
可以是OuterDirective
范围提供的任意变量。 The =
means that updating that value in the InnerDirective
's scope will update it as well in the OuterDirective
's scope. 的
=
意味着更新在该值InnerDirective
的范围将在更新以及OuterDirective
适适用范围“。
The second case is possible, you can pass everything you want, one-way, to an attribute marked by &
. 第二种情况是可能的,您可以将所需的所有内容单向传递给
&
标记的属性。
Using temp
in both inner and outer scope is a bit confusing, so I'm going to use outer
and inner
instead. 在内部和外部范围中都使用
temp
会造成一些混乱,因此我将改为使用outer
和inner
。
Let's suppose outer()
is a function defined in your outer scope, returning an arbitrary result you want to display in your directive and inner
is your inner directive attribute. 假设
outer()
是在外部范围中定义的函数,返回要在指令中显示的任意结果,而inner
是您的内部指令属性。
<outer-directive>
<inner-directive inner="{{outer()}}" />
</outer-directive>
This will work like a charm if you want one-way binding. 如果您要单向绑定,这将像一种魅力。 The confusing part is that
&
attributes need {{}}
to bind to a value, when =
attributes don't. 令人困惑的部分是
&
属性需要{{}}
绑定到一个值,而=
属性则不需要。
Now you can use 现在您可以使用
<div>{{inner}}</div>
directly. 直。
You can also pass your outer
function so that it is called inside your inner directive, but that's a bit trickier. 您还可以传递
outer
函数,以便在内部指令内部调用它,但这有点棘手。
If you want to know more about this, I recommand you read the "Creating a Directive that Wraps Other Elements" in Angular's documentation about directives , there is a very good example of what it can do at the end of the section (you want to look at the very last example). 如果您想了解更多信息,建议您阅读Angular文档中有关指令的“创建包含其他元素的指令” ,本节末尾有一个很好的示例(您想请看最后一个示例)。
Here is a brief excerpt : 这是一个简短的摘录:
We saw earlier how to use =attr in the scope option, but in the above example, we're using &attr instead.
我们之前已经看到了如何在scope选项中使用= attr,但是在上面的示例中,我们改为使用&attr。 The & binding allows a directive to trigger evaluation of an expression in the context of the original scope, at a specific time.
&绑定允许指令在特定时间在原始范围的上下文中触发对表达式的求值。 Any legal expression is allowed, including an expression which contains a function call.
允许使用任何合法表达式,包括包含函数调用的表达式。 Because of this, & bindings are ideal for binding callback functions to directive behaviors.
因此,&绑定是将回调函数绑定到指令行为的理想选择。
Best Practice: use &attr in the scope option when you want your directive to expose an API for binding to behaviors.
最佳实践:当您希望指令公开用于绑定行为的API时,请在scope选项中使用&attr。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.