0

I hope I can make sense with this question

is it possible to alter your text label within Angular .html layout depending on some criteria in your query? For example below, possibly I'd like to interactively alter the "placeholder="Service Date" to something other than 'Service Date', depending on some criteria in my query in the corresponding .ts file, some indicator field. Effectively, if indicator='x' then placeholder="something else"

is that possible to accomplish?

<div class="row">
    <mat-form-field>
       <input matInput placeholder="Service Date" name="serviceDate" value="
          {{claim.serviceDate | date}}" disabled="disabled">
    </mat-form-field>
</div>
2
  • Well, you're able to alter the value, right? Why not try a similar technique with placeholder? Commented Mar 15, 2018 at 21:36
  • Did you try placeholder="{{indicator=='x'? 'something else':'Service Date'}}" Commented Mar 15, 2018 at 21:41

2 Answers 2

1

By setting the placeholder as a binding you can then use an expression that can be evaluated either in the component or the view itself

<input matInput [placeholder]=“<any expression>” name="serviceDate" value="
      {{claim.serviceDate | date}}" disabled="disabled">

Where <any expression> can be a variable, a ternary expression or anything that can be evaluated.

Sign up to request clarification or add additional context in comments.

Comments

0

It is good practice to move(wrap) some expression logic into method of class and use it in template. It's helps to keep template clean and easy to test.

Template:

<div class="row">
        <mat-form-field>
           <input matInput placeholder="{{getPlaceholder()}}" name="serviceDate" value="
              {{claim.serviceDate | date}}" disabled="disabled">
        </mat-form-field>
    </div>

Component class:

  class SomeComponent:
    {
      getPlaceholder(): string {
        return this.indicator == x ? "placeHolder1" : "placeHolder2";
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.