angularjs翻译占位符

时间:2015-09-24 09:55:09

标签: angularjs angular-translate

我正在使用角度转换器来翻译我的html按钮和标签,问题是当我在占位符中使用时,我正在为法语使用垃圾字符。但同样在span标签中运行良好。占位符会出现什么问题。

public static void main(String... args) throws Exception {   
        IMOperation op = new IMOperation();
        op.addImage();                       
        op.resize(350)
        op.addImage("png:-")               
        BufferedImage images = ImageIO.read(new File("image.jpg")); 

        // set up command
        ConvertCmd convert = new ConvertCmd();
        Stream2BufferedImage s2b = new Stream2BufferedImage();
        convert.setOutputConsumer(s2b);

        // run command and extract BufferedImage from OutputConsumer
        convert.run(op,images);
        BufferedImage img = s2b.getImage();
    }

enter image description here

同样的东西正在使用span

  <input data-ng-model="vm.search" class="form-control"  type="search"
                           ng-attr-placeholder="{{'resources.contacts-organisations-searchplaceholder' | translate }}..."> 

enter image description here

2 个答案:

答案 0 :(得分:3)

对不起晚了: 但我为此创建了一个解决您问题的指令。

    app.directive('ngBindPlaceholder', ['$sce', '$parse', '$compile', function($sce, $parse, $compile){
   return {
    restrict: 'A',
    compile: function ngBindHtmlCompile(tElement, tAttrs) {
      var ngBindHtmlGetter = $parse(tAttrs.ngBindPlaceholder);
      var ngBindPlaceholderWatch = $parse(tAttrs.ngBindPlaceholder, function getStringValue(value) {
        return (value || '').toString();
      });
      $compile.$$addBindingClass(tElement);

      return function ngBindHtmlLink(scope, element, attr) {
        $compile.$$addBindingInfo(element, attr.ngBindPlaceholder);

        scope.$watch(ngBindPlaceholderWatch, function ngBindWatchAction() {
          // we re-evaluate the expr because we want a TrustedValueHolderType
          // for $sce, not a string
          element.html($sce.getTrustedHtml(ngBindHtmlGetter(scope)) || '');
          var plc = element.text();
          element.attr('placeholder',plc);
        });
      };
    }
  };
}]);

在HTML中使用:

<input type="text" id = "name" data-ng-bind-placeholder="'Label.name' | translate>

答案 1 :(得分:0)

您确定语法是否正确?似乎{{'resources.contacts-organisations-searchplaceholder' | translate }}将在此对象的属性中传递字符串resources.contacts-organisations-searchplaceholder而不是字符串