techie_questie
techie_questie

Reputation: 1522

How to move *ngIf conditions from template to typescript file in angular 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?

Upvotes: 2

Views: 2471

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658087

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

  get placeholder() {
    if (cond1 || cond2) {
      return 'YES';
    } else if (cond3 || cond4 || cond5) {
      return 'NO';
    }
  }
}

Upvotes: 5

Related Questions