简体   繁体   English

角度转换自定义属性

[英]Angular translate on custom attributes

I have a switch widget which uses custom data attribute value to label itself. 我有一个开关小部件,它使用自定义数据属性值来标记自身。

 .switch.switch-text .switch-label::before { right: 1px; color: #c2cfd6; content: attr(data-hide); } .switch.switch-text .switch-label::after { left: 1px; color: #c2cfd6; content: attr(data-show); opacity: 0; } .switch.switch-text .switch-input:checked ~ .switch-label::before { opacity: 0; } .switch.switch-text .switch-input:checked ~ .switch-label::after { opacity: 1; } 
 <label class="switch switch-text switch-pill switch-primary"> <input type="checkbox" class="switch-input" checked> <span class="switch-label" attr.data-show="{{GLOBALS.ACTIONS.SHOW | translate}}" attr.data-hide="{{GLOBALS.ACTIONS.HIDE | translate}}"></span> <span class="switch-handle"></span> </label> 

But it just does not work. 但这是行不通的。 I have looked at different answers relating to similar problem but some says it works some says it doesn't. 我已经看过与类似问题有关的不同答案,但有些人说可以,有些人说没有。 If I user without attr. 如果我没有attr的用户。 in front I get a binding error anyway since it does not recognise the attribute. 在前面,无论如何我都会遇到绑定错误,因为它无法识别该属性。

How can I translate the value of custom attribute using angular translate? 如何使用角度转换转换自定义属性的值?

You just have a typo in your template. 您的模板中只有一个错字。 Your have to use a one way data binding syntax to update attributes "data-show" with the translated value. 您必须使用一种数据绑定语法来用转换后的值更新属性“ data-show”。 If you omit the bracket then you just create a static " attr.data-show " attribute whom value is " {{GLOBALS.ACTIONS.SHOW | translate}} " 如果省略括号,则只需创建一个静态“ attr.data-show ”属性,其值为“ {{GLOBALS.ACTIONS.SHOW | translate}}

Your code produces : 您的代码产生:

<span class="switch-label" attr.data-show="{{GLOBALS.ACTIONS.SHOW | translate}}" attr.data-hide="{{GLOBALS.ACTIONS.HIDE | translate}}"></span>

The corrected template syntax is : 更正后的模板语法为:

  <label class="switch switch-text switch-pill switch-primary">
        <input type="checkbox" class="switch-input" checked>
        <span class="switch-label" [attr.data-show]="'GLOBALS.ACTIONS.SHOW' | translate" [attr.data-hide]="'GLOBALS.ACTIONS.HIDE' | translate"></span>
        <span class="switch-handle"></span>
   </label>

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

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