在角度为什么类和注释指令没有绑定?

时间:2013-06-01 01:06:45

标签: angularjs angularjs-directive

Plunker:http://plnkr.co/edit/XBJjr6uR1rMAg3Ng7DiJ?p=preview

我希望这两个看起来一样:

<span dt-attrib="{{person.name}}">This won't be here</span><br/>
<span class="dt-class: {{person.name}};">This won't be here.</span> - this does not process

但这就是我得到的:

This text was set by the dtAttrib directive, value="Burt Finklestein"
This text was set by the dtClass directive, value="{{person.name}}"

代码:

app.directive("dtAttrib", function() {
    return {
        restrict: "A",
        link: function(scope, element, attrs) {
            element.text('This text was set by the dtAttrib directive' + DisplayValueString(attrs.dtAttrib));
            }
        }
    }
});

app.directive("dtClass", function() {
    return {
        restrict: "C",
        link: function(scope, element, attrs) {
            element.text('This text was set by the dtClass directive' + DisplayValueString(attrs.dtClass));
        }
    };
});

1 个答案:

答案 0 :(得分:1)

实际上你用你的<span class="dt-class-b: person.name;">This won't be here.</span> - this DOES process, with no curly braces线在你自己的羽毛球上击中了钉子。类指令采用C - 类形式:<div class="my-directive: exp;"></div>(参见http://docs.angularjs.org/guide/directive),这意味着该指令被传递给解析器并且不会在大括号解析阶段从那里绑定。

您在属性情况下看到它处理的原因是预处理器(角度核心指令编译器)不知道跳过您创建的属性,因此它仍然处理它然后将值作为表达式发送返回。