2

I am new to angular 2.I have to display Yes and No label based on certain conditions.The same label should get replaced.For that,I am writing *ngIf condition in my template file with OR operators.It makes my template look clumsy so I want to declare those *ngIf condition in my typescript file and use the variable instead in my template with *ngIf.

Also, is there any way to achieve 'Yes' and 'No' placeholders with one md-input?If yes, then how to do that?

My template-

<md-input *ngIf="cond1 || cond2" placeholder="Yes"></md-input>
<md-input *ngIf="cond3||cond4||cond5" placeholder="No"></md-input>

<input type="button" [disabled]="placeholder==='YES'" value="Save">

How to do that?

0

1 Answer 1

5
<md-input [placeholder]="placeholder"></md-input>
class MyComponent {

  get placeholder() {
    if (cond1 || cond2) {
      return 'YES';
    } else if (cond3 || cond4 || cond5) {
      return 'NO';
    }
  }
}
Sign up to request clarification or add additional context in comments.

11 Comments

Can we use get like that or it should be a function?
You can remove get and then use it like [placeholder]="placeholder()". This way you also can pass parameters.
cool! but the value is not changing now.Its always showing Yes.
Seems to indicate that cond1 || cond2 is always true
cond 1 to cond 5 are select dropdown values.The placeholder value should get updated based on the value selected from the dropdown.
|

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.