2

I am using directive selector on click event which give asc and desc but i want to remove this directive while my reactive form is dirty. appSortable is directive selector

<div class="bank-name sort" appSortable sortDirection="asc" (sorted)="onSortedBankName($event)" (click)="sortColumnBy(bankConstants.sortBy.name)"  #Sortable>
Sachin Gupta
  • 4,981
  • 3
  • 16
  • 32
prabin shrestha
  • 75
  • 1
  • 10
  • Possible duplicate of [Apply a directive conditionally](https://stackoverflow.com/questions/44597077/apply-a-directive-conditionally) – Vega Apr 19 '19 at 02:37

4 Answers4

2

Why not using *ngIf in your template?

<div *ngIf="myForm.dirty" class="bank-name sort">
<div *ngIf="!myForm.dirty" class="bank-name sort" appSortable sortDirection="asc" (sorted)="onSortedBankName($event)" (click)="sortColumnBy(bankConstants.sortBy.name)"  #Sortable>
eess
  • 21
  • 3
  • 1
    in click event i need to check form is dirty or not then warning model will pupup what if user Click 'YES" how to store previous sortDirection = (asc or desc) and sortBy – prabin shrestha Apr 19 '19 at 05:11
0

Can you not update your directive to accept an enabled property and then set it to the state of the form?

[isEnabled]="!myForm.dirty"

Have your directive do nothing if this value is false.

0

Having two similar codes is fine only if the code is 1-liner. If it spans multiple rows, the code can get too ugly too soon.

You can have an input parameter to your directive. You can pass the value of form.dirty to the directive and apply whatever logic you want there. It also saves you from duplicating the code.

See example here

Edit: Made the solution more generic, Each component can have its own function and sort accordingly.

Sachin Gupta
  • 4,981
  • 3
  • 16
  • 32
0

You can use below code to hide your content if form is dirty

<div *ngIf="!YourFormName.dirty" class="bank-name sort" appSortable sortDirection="asc" (sorted)="onSortedBankName($event)" (click)="sortColumnBy(bankConstants.sortBy.name)"  #Sortable>