在下面的插件中:
http://plnkr.co/edit/ss3HTb?p=info
我正在尝试将属性绑定到隔离范围。但是,它不起作用。我想知道是否有人知道为什么会这样?
我查看了几个资源,包括: Need some examples of binding attributes in custom AngularJS tags和http://www.sitepoint.com/practical-guide-angularjs-directives-part-two/
答案 0 :(得分:2)
您将隔离范围指定为
scope: {
attr: "=attributeName"
}
这样做是将html中指定的attributeName
的值作为范围中的表达式,并将其绑定到指令范围中的attr
。但是你所做的只是在你的范围内指定"hello"
而没有hello
对象。为了让它按照您现在的方式工作,您必须使用@
属性绑定,如下所示:
scope: {
attr: "@attributeName"
}
您需要做的另一项更改是如何在html中指定值。您在定义属性时使用titleCase,但在html而不是titleCase中,您需要分隔"单词"用短划线并使用全小写。因此attributeName
变为attribute-name
<node attribute-name="hello"></node>
另一方面,如果您确实希望绑定到作用域中对象的值,请确保在作用域中指定它,然后可以在指令中使用它(and preferably use the dot notation)
我们已更新您的plunker,告诉您它们的工作原理。