Javascript Angular 2 Material Input 动态更改占位符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44307751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:25:57  来源:igfitidea点击:

Angular 2 Material Input change placeholder dynamically

javascriptangulartypescriptangular-materialplaceholder

提问by maidi

I want to change the text of the input placeholder dynamically. The console.log already gives the updated string but the interface doesn't update so there stays the old placeholder. How can I get the Interface to recognize the change?

我想动态更改输入占位符的文本。console.log 已经提供了更新的字符串,但界面没有更新,所以保留了旧的占位符。我怎样才能让界面识别变化?

document.getElementById(this.implicKey).setAttribute('placeholder', options[i].implication);

console.log(document.getElementById(this.implicKey).getAttribute('placeholder'));

回答by CharanRoot

you can change your input placeholder dynamically like this

您可以像这样动态更改输入占位符

<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>

component.ts

组件.ts

somePlaceholder : string = "new value";

now you can change somePlaceholder value any where in the class.

现在您可以在类中的任何位置更改 somePlaceholder 值。

回答by EQuadrado

We can do that using property binding.

我们可以使用属性绑定来做到这一点。

In the HTML, use square brackets:

在 HTML 中,使用方括号:

<input formControlName="events" type="text" [placeholder]="newPlaceHolder">

In your typescript file, define the property:

在您的打字稿文件中,定义属性:

newPlaceHolder: string = "original place holder";

Then, change the property value:

然后,更改属性值:

newPlaceHolder= "my new place holder";