如何将自定义指令的属性值绑定到一个孤立的作用域

How to bind a custom directive attribute value to an isolated scope in angular

本文关键字:一个 作用域 绑定 自定义 指令 属性      更新时间:2023-09-26
    app.directive('myCustomAttr',function() {
    return {
        restrict: 'A',
        scope: {
            valueOfAttr : "@myCustomAttr"
        },
      };
});

如何传递属性的值?到目前为止,我只发现了使用restrict: 'E'的例子。

<input type="text" my-custom-attr="myValue" />

那么,如果我要绑定"myValue"到作用域,我该怎么做呢?

[编辑]

对不起,我打错了。我正确地使用了my-custom属性,但它似乎仍然没有在指令中绑定。

我很确定你想要=myCustomAttr而不是@myCustomAttr。在"隔离指令的范围"中提到了这一点。这里还有更多信息

使用链接函数:

link: function (scope, element, attrs) {
   var myAttr = attrs["myCustomAttr"];
}