简体   繁体   English

通过自定义指令使用角度转换

[英]Using angular translate with custom directive

Currently I am creating my own directive using the angular translate. 目前,我正在使用角度转换创建自己的指令。 As a tooltip for the image I am using some translation. 作为图像的提示,我正在使用一些翻译。 When I am switching the language, the translation text stays the same, when it should change. 当我切换语言时,应更改的翻译文本保持不变。 I think, that the directive got compiled once with the current translation value, but I don't know how to get it updated any time I switch my language (some kind recompile). 我认为,该指令使用当前的转换值编译一次,但是我不知道如何在切换语言时进行某种方式的更新(某种形式的重新编译)。

Below my directive code. 下面是我的指令代码。

 'use strict'; angular.module('HomeModule') .directive("headerName", ["tabService", "imageService", "$translate", "$filter", function(tabService, imageService, $translate, $filter){ var directive = {}; directive.restrict = 'E'; directive.template = '<div class="vcenter"><img ng-src="' + imageService.getImageByKey('arrowLeftSrc') + '" height="20px" tooltip-class="custom-tooltip" tooltip="' + $filter('translate')('RETURN') + '" tooltip-placement="bottom" tooltip-popup-delay="500" class="return-arrow"/></div>' + '<div class="vcenter"><i class="fa fa-angle-down fa-2x"/></div>'; directive.scope = {} return directive; }]); 

In short the solution is: translate-attr-tooltip="RETURN" . 简而言之,解决方案是: translate-attr-tooltip="RETURN"

Don't use a $filter in your directive template, but the angular-translate directive translate-attr-* ( https://angular-translate.github.io/docs/#/api/pascalprecht.translate.directive:translate ). 不要在指令模板中使用$filter ,而是angular-translate指令translate-attr-*https://angular-translate.github.io/docs/#/api/pascalprecht.translate.directive:translate ) 。

Here is a JSFiddle to play around - http://jsfiddle.net/ku326j6p/2/ 这是一个JSFiddle可以玩-http: //jsfiddle.net/ku326j6p/2/

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

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