Javascript 角度2材质输入动态更改占位符
我想动态更改输入占位符的文本。 console.log已经提供了更新的字符串,但是接口没有更新,因此保留了旧的占位符。 如何让界面识别更改Javascript 角度2材质输入动态更改占位符,javascript,angular,typescript,angular-material,placeholder,Javascript,Angular,Typescript,Angular Material,Placeholder,我想动态更改输入占位符的文本。 console.log已经提供了更新的字符串,但是接口没有更新,因此保留了旧的占位符。 如何让界面识别更改 document.getElementById(this.implicKey).setAttribute('placeholder', options[i].implication); console.log(document.getElementById(this.implicKey).getAttribute('placeholder')); 您可以
document.getElementById(this.implicKey).setAttribute('placeholder', options[i].implication);
console.log(document.getElementById(this.implicKey).getAttribute('placeholder'));
您可以像这样动态更改输入占位符
<md-input-container class="demo-full-width">
<input mdInput [(ngModel)]="firstname" placeholder="{{somePlaceholder}}" name="firstname" required>
<md-error>This field is required</md-error>
</md-input-container>
现在,您可以在类中的任何位置更改占位符值。我们可以使用属性绑定来实现这一点 在HTML中,使用方括号:
<input formControlName="events" type="text" [placeholder]="newPlaceHolder">
然后,更改属性值:
newPlaceHolder= "my new place holder";
这个对我来说很有用: (我用它在mat自动完成表单字段上显示动态错误) 在您的HTML上:
[placeholder]="isPlaceHolderShowError('myFormControlName')"
在您的TS上:
isPlaceHolderShowError(myFormControlName) {
if (this.form.controls[myFormControlName].invalid && this.form.controls[myFormControlName].touched) {
return 'this is my error text'
}
return 'this is the initial placehloder'
}
为什么要使用document.getElementById您可以使用Bindings进行更改我添加了此函数setPlaceholder(value:string){this.somePlaceholder=value;},并将其作为闭包传递给我的服务,以便在数据到达时设置占位符。但它说这是未定义的。我做错了什么?服务的订阅发生在服务类本身内部,我在成功时调用闭包方法。
[placeholder]="isPlaceHolderShowError('myFormControlName')"
isPlaceHolderShowError(myFormControlName) {
if (this.form.controls[myFormControlName].invalid && this.form.controls[myFormControlName].touched) {
return 'this is my error text'
}
return 'this is the initial placehloder'
}