1

我一直在构建一些角度组件,想知道单向(“<”)和属性(“@”)绑定之间的实际区别是什么?

是否存在一种比另一种更可取的情况?很简单,属性总是不可变的字符串,必须插值?

4

1 回答 1

1

From https://docs.angularjs.org/guide/component:

@ bindings can be used when the input is a string, especially when the value of the binding doesn't change.

From https://docs.angularjs.org/api/ng/service/$compile#-scope-

"@" - bind a local scope property to the value of DOM attribute. The result is always a string since DOM attributes are strings.

"<" - set up a one-way (one-directional) binding between a local scope property and an expression passed via the attribute

So, use '@' for passing string values. The value could be a simple string value, e.g. myattr="hello", or it could be an AngularJS interpolated string with embedded expressions, e.g. myattr="my_{{helloText}}".

于 2016-07-06T06:19:18.747 回答