Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Material Input change placeholder dynamically

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?

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

console.log(document.getElementById(this.implicKey).getAttribute('placeholder'));
like image 418
maidi Avatar asked Jun 01 '17 12:06

maidi


2 Answers

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

somePlaceholder : string = "new value";

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

like image 133
CharanRoot Avatar answered Sep 16 '22 16:09

CharanRoot


We can do that using property binding.

In the HTML, use square brackets:

<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";
like image 20
EQuadrado Avatar answered Sep 20 '22 16:09

EQuadrado