1

I have a dialog Box which has some buttons an i can set the primary="true" to make it the default answer for the user. In my case the value for primarryButton is button1 and button2 and i want the have it set if it is primary="true" the other button will have nothing.

<div>
        <button class="button" kendoButton (click)="onCancelAction()" >{{Button2}}</button>
        <button class="button" kendoButton (click)="onConfirmAction()" >{{Button1}}</button>
</div>

so the ngif should say in the line for Button1

*ngIf="primaryButton==='button1'" then primary="true"

but not sure how to accomplish that

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
NoSoup4you
  • 640
  • 1
  • 10
  • 34
  • You could try setting the attribute with the value being the check itself, e.g. `` This will give you true or false for that attribute. – Eraph Oct 01 '19 at 00:06
  • i was thinking of that but the code does not like it when you have a primary="false" so it expects primary="true" or nothing which doesnt work in this case – NoSoup4you Oct 01 '19 at 00:08
  • 1
    According to https://stackoverflow.com/questions/36745734/how-to-add-conditional-attribute-in-angular-2, you can hide an attribute by setting its value to null, so to expand on the previous slice of code: `` Note that `attr.primary` must be wrapped in square brackets. – Eraph Oct 01 '19 at 00:22
  • That does not produce the expected output.. – NoSoup4you Oct 01 '19 at 03:52

3 Answers3

2

Bearly understand what u want to achieve try this:

  <kendo-dialog-actions>
      <div>
        <ng-container *ngIf="primaryButton == 'Button1'; then but1; else but2"></ng-container>
          <ng-template #but1>
              <button class="button" kendoButton (click)="onCancelAction()">{{Button2}}</button>
              <button class="button" kendoButton (click)="onConfirmAction()" primary="true" >{{Button1}}</button>
          </ng-template>
          <ng-template #but2>
              <button class="button" kendoButton (click)="onCancelAction()" primary="true">{{Button2}}</button>
              <button class="button" kendoButton (click)="onConfirmAction()" >{{Button1}}</button>
          </ng-template>
      </div>
    </kendo-dialog-actions>
NoSoup4you
  • 640
  • 1
  • 10
  • 34
Mises
  • 4,251
  • 2
  • 19
  • 32
0

Instead of adding ngIf on custom primaryButton attribute on your element why don't you play with class itself.

If still you need attribute only then let me know.. I will update you as per your requirement then.

vijay sahu
  • 765
  • 1
  • 7
  • 29
0

You don't need to use ngIf at all here as already mentioned in the comments. In Angular, if you pass null to an attribute binding, it does not get rendered. So we can just add a ternary expression to the primary attribute using attribute binding and conditionally pass true or null.

<kendo-dialog-actions>
    <div>
        <button class="button" [attr.primary]="primaryButton === 'Button1' ? true : null" kendoButton (click)="onConfirmAction()">{{Button1}}</button>
        <button class="button" [attr.primary]="primaryButton === 'Button2' ? true : null" kendoButton (click)="onCancelAction()">{{Button2}}</button>
    </div>
</kendo-dialog-actions>
nash11
  • 8,220
  • 3
  • 19
  • 55