[英]Angular Material Input not updating placeholder text
I am using Angular 6 with Material CSS from material.angular.io我正在使用来自 material.angular.io 的带有 Material CSS 的 Angular 6
I am trying to create my directive that will offer translation.我正在尝试创建将提供翻译的指令。
<span translate>page.login.btnsubmit</span>
is working fine as the text inside tags are getting translated.工作正常,因为标签内的文本正在翻译。
To translate the attribute placeholder翻译属性占位符
Below is working下面正在工作
<input type="text" translate="placeholder" placeholder="newworld">
Below is not working下面不起作用
<input type="text" matInput placeholder="Username / Email / Mobile" value="" translate="placeholder">
It is because of input matInput attribute, that is not taking the updated value.这是因为输入 matInput 属性,没有采用更新的值。
What are the possibilities i could update the matInput attributes.我可以更新 matInput 属性的可能性有哪些。 I action hook i use on directive is ngAfterViewInit
我在指令上使用的动作钩子是 ngAfterViewInit
Thanks.谢谢。
EDIT 1 - TRANSLATE DIRECTIVE TS编辑 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];
}
)
}
}
To change the placeholder in a matInput use mat-placeHolder, like要更改 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>
First of all your code is working fine and setting the placeholder
of input
box.首先,您的代码工作正常并设置了
input
框的placeholder
。 What you see on the screen is the div
placeholder
created by matInput
directive.您在屏幕上看到的是由
matInput
指令创建的div
placeholder
。
Whenever matInput
directive is issued, we need to change the placeholder
attribute
of MatInput
so that it will change the placeholder
`div.无论何时发出
matInput
指令,我们都需要更改MatInput
的placeholder
attribute
,以便它更改placeholder
`div。
Follow the steps to change the
placeholder
property ofMatInput
按照步骤更改
MatInput
的placeholder
属性
#matInput="matInput"
#matInput="matInput"
Set the reference of matInput in html so that it can be accessed in the translate
directive
.在 html 中设置 matInput 的引用,以便它可以在
translate
directive
访问。
<input type="text" matInput #matInput="matInput" placeholder="Original
Placeholder" [(ngModel)]="user.username" translate="placeholder">
placeholder
of matInput
model. matInput
模型的placeholder
。 Access the matInput
directive in translate
directive在
translate
指令中访问matInput
指令
@ContentChild("matInput") matInput : MatInput;
Change the placeholder of matInput
更改
matInput
的占位符
this.matInput.placeholder = "Translated placeholder";
Working copy is here https://stackblitz.com/edit/angular-q1ufxj工作副本在这里https://stackblitz.com/edit/angular-q1ufxj
In my case only something like that helped:在我的情况下,只有这样的帮助:
ngAfterViewChecked(): void {
this.setPlaceHolder();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.