简体   繁体   English

为什么这个角度表达式在我的模板指令中不起作用

[英]Why doesn't this angular expression work in my template's directive

I have a directive that uses this template: 我有一个使用此模板的指令:

template: '<div id={{day}} ng-class="daystatus" ng-click="toggle()">{{day | date: "d"}}</div>'

the variable date contains a day which when executed renders as follows: 可变日期包含执行时的日期,如下所示:

<div id="&quot;2015-09-21T22:00:00.000Z&quot;" ng-class="daystatus" ng-click="toggle()" class="ng-binding selectable" role="button" tabindex="0">22</div>

Question one: "Why is is placing the double quotes in the string in the first expression? 问题一:“为什么在第一个表达式的字符串中加上双引号?

Question two: As you can see, in this case everything works as expected, but if I try to apply in the first expression to get the same formating as in the second one as such as: 问题二:如您所见,在这种情况下,一切都按预期工作,但是如果我尝试在第一个表达式中应用以得到与第二个表达式相同的格式,例如:

template: '<div id={{day | date: "d"}} ng-class="daystatus" ng-click="toggle()">{{day | date: "d"}}</div>'

it simply does not render as expected producing the following HTML code: 它根本无法按预期方式生成以下HTML代码:

<div id="{{day" |="" date:"d"}}="" ng-class="daystatus" ng-click="toggle()" class="ng-binding selectable" role="button" tabindex="0">22</div>

Any clue on why it is behaving like this? 关于它为何如此行为的任何线索?

You must escape the quotes in the attribute: 您必须对属性中的引号进行转义:

{
    template: '<div id={{day | date: "d"}} ng-class="daystatus" ng-click="toggle()">{{day | date: "d"}}</div>'
}

To: 至:

{
    template: '<div id="{{day | date: \'d\'}}" ng-class="daystatus" ng-click="toggle()">{{day | date: "d"}}</div>'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM