javascript 何时以及为何使用 &?, =?, @? 在 AngularJS 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31589316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When and why to use &?, =?, @? in AngularJS?
提问by Shaohao Lin
I followed some tutorials to create angular directives. In the isolate scope, some tutorials define scope like this:
我按照一些教程来创建角度指令。在隔离范围中,一些教程定义范围是这样的:
scope: {
model: '=?',
data: '@?'
}
Meanwhile, some tutorials define scope without question mark like this:
同时,一些教程定义了没有问号的范围,如下所示:
scope: {
model: '=',
data: '@'
}
Can anybody explain me the difference or the purpose of these with examples? Thank you.
谁能用例子向我解释这些的区别或目的?谢谢你。
回答by Daniel Cottone
The &
, @
, and =
symbols are used to define the bindings (one-way, bi-directional, etc) for isolated scope objects, as you already know. Here is a pretty thorough tutorial on how all this works.
的&
,@
和=
符号用于定义绑定(单向,双向等),用于分离范围的对象,因为你已经知道了。这是关于所有这些如何工作的非常详尽的教程。
The ?
symbol is used to indicate that the parent scope property to which the isolated scope binding refers to is optional. This means that if for some reason the parent scope property doesn't exist, then your application will continue to run without throwing the NON_ASSIGNABLE_MODEL_EXPRESSION exception.
该?
符号用于指示隔离作用域绑定所引用的父作用域属性是可选的。这意味着如果由于某种原因父作用域属性不存在,那么您的应用程序将继续运行而不会抛出 NON_ASSIGNABLE_MODEL_EXPRESSION 异常。