gpt4 book ai didi

javascript - Angular 隔离范围和属性

转载 作者:行者123 更新时间:2023-12-02 17:22:12 24 4
gpt4 key购买 nike

在下面的代码中:

http://plnkr.co/edit/ss3HTb?p=info

我正在尝试将属性绑定(bind)到隔离范围。但是,它不起作用。我想知道是否有人知道这是为什么?

我浏览了多种资源,包括: Need some examples of binding attributes in custom AngularJS tagshttp://www.sitepoint.com/practical-guide-angularjs-directives-part-two/

最佳答案

您将隔离范围指定为

scope: {
attr: "=attributeName"
}

它的作用是将 html 中指定的 attributeName 值作为作用域中的表达式,并将其绑定(bind)到指令作用域中的 attr 。但您所做的只是指定 "hello" ,而您的作用域中没有 hello 对象。要让它按照现在的方式工作,您必须使用 @ 属性绑定(bind),如下所示:

scope: {
attr: "@attributeName"
}

您需要做的另一个更改是如何在 html 中指定值。定义属性时使用 titleCase,但在 html 中,您需要用破折号分隔“单词”而不是 titleCase,并全部使用小写。因此 attributeName 变为 attribute-name

<node attribute-name="hello"></node>

另一方面,如果您确实想绑定(bind)到作用域中对象的值,请确保在作用域中指定它,然后可以在指令中使用它( and preferably use the dot notation )。

我已更新您的plunker向您展示它们是如何工作的。

关于javascript - Angular 隔离范围和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com