【问题标题】:Angular Material Input not updating placeholder text角度材质输入不更新占位符文本
【发布时间】:2019-03-25 00:31:50
【问题描述】:

我正在使用来自 material.angular.io 的 Angular 6 和 Material CSS

我正在尝试创建将提供翻译的指令。

<span translate>page.login.btnsubmit</span>

标签内的文本正在翻译,工作正常。

翻译属性占位符

以下工作正常

<input type="text" translate="placeholder" placeholder="newworld">

以下不工作

<input type="text" matInput placeholder="Username / Email / Mobile" value="" translate="placeholder">

由于输入 matInput 属性,没有取更新值。

我可以更新 matInput 属性的可能性有哪些。我在指令上使用的动作钩子是 ngAfterViewInit

谢谢。

编辑 1 - 翻译指令 TS

import { Directive, ElementRef, Input } from '@angular/core';
import * as _ from 'lodash';
import { TranslateService } from '../services/translate.service';

@Directive({
  selector: '[translate]'
})
export class TranslateDirective {

  @Input('translate') referrer:string;

  constructor(private el: ElementRef, private ts: TranslateService) {

  }

  ngAfterViewInit() {
    if(_.isEmpty(this.referrer)){ // executed for text between the tags example: <span translate>page.login.username</span>
      this.translateInnerHtml()
    }else{ // executed for attributes
      this.translateAttribute();
    }
    return;
  }

  translateAttribute(){ // Not updating
    this.el.nativeElement.attributes.placeholder.value="dynamic placeholder";
  }

  translateInnerHtml(){ // Working fine as expected
    const innerHtml = this.el.nativeElement.innerHTML;
    if (_.isEmpty(innerHtml)) {
      this.el.nativeElement.innerHTML = 'No text provided for translation';
      return;
    }
    this.getTranslationText(innerHtml)
  }


  getTranslationText(text){
    let splitText = _.split(text, '.');
    let key = _.join(splitText.slice(-1));
    let file = _.join(splitText.slice(-2, -1));
    let folder = _.join(splitText.slice(0, splitText.length - 2), '/');
    if (_.isEmpty(key) || _.isEmpty(file) || _.isEmpty(folder)) {
      this.el.nativeElement.innerHTML = 'No text provided for translation';
      return;
    }
    this.ts.get(folder, file).subscribe(
      (data) => {
        if (_.isEmpty(data) || _.isEmpty(data[key])) {
          this.el.nativeElement.innerHTML = 'No text provided for translation';
          return;
        }
        this.el.nativeElement.innerHTML = data[key];
      }
    )
  }

}

Adding stackblitz code

【问题讨论】:

  • try [placeholder]=" ' ....' " (我在引号之间放置了空格以便于阅读,但您必须删除。
  • 我添加了 stackblitz 代码。请检查一下。

标签: angular


【解决方案1】:

要更改 matInput 中的占位符,请使用 mat-placeHolder,例如

<mat-form-field class="username">
     <input type="text" matInput [(ngModel)]="user.username" >
     <mat-placeholder>
        <span translate>page.login.btnsubmit</span>
     </mat-placeholder>
</mat-form-field>

【讨论】:

  • 在 mat-chip-list 和 mat-autocomplete 中我不得不像这样使用翻译管道:[placeholder]="'receivers' | translate"
【解决方案2】:

问题

首先,您的代码工作正常并设置了 placeholderinput 框。你在屏幕上看到的是由matInput 指令创建的div placeholder

修复

每当matInput指令发出时,我们需要改变MatInputplaceholderattribute,这样它就会改变placeholder`div。

按照步骤更改MatInputplaceholder 属性

1。设置matInput的引用为#matInput="matInput"

在html中设置matInput的引用,以便可以在translatedirective中访问。

 <input type="text" matInput #matInput="matInput" placeholder="Original 
     Placeholder" [(ngModel)]="user.username" translate="placeholder"> 

2。更改matInput 模型的placeholder

访问translate指令中的matInput指令

 @ContentChild("matInput") matInput : MatInput;

更改matInput的占位符

this.matInput.placeholder = "Translated placeholder";

工作副本在这里https://stackblitz.com/edit/angular-q1ufxj

【讨论】:

  • 它与matinput有关。我没有传递任何密钥,而是像硬编码一样传递翻译文本。 this.el.nativeelement.attributes.placeholder="newworld"
  • 您尝试翻译的文本可能有问题。你能把Username / Email / Mobile 改成`newworld`并检查是否有效。
  • 。请注意,我没有翻译任何文本,出于测试目的,我使用静态文本替换占位符属性中的现有文本。与文字无关
  • 现在说得通了。你能分享translate指令的代码吗?
  • 我还创建了一个 stackblitz 代码供您参考
【解决方案3】:

就我而言,只有这样的帮助:

ngAfterViewChecked(): void {
    this.setPlaceHolder();
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2015-12-11
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多