Directives not working with FormControl after upgrading to Angular 9

随声附和 提交于 2020-03-02 12:23:06

问题


I have used directives for enabling and disabling forms. This is in a separate typescript file. Code is given below: -

import { NgControl } from '@angular/forms';
import { Directive, Input } from '@angular/core';

@Directive({
    selector: '[disableControl]'
})
export class DisableControlDirective {
    @Input('disableControl') set disableControl( condition : boolean ) {
        const action = condition ? 'disable' : 'enable';
        this.ngControl.control[action]();
    }
    constructor (private ngControl : NgControl){}
}

HTML: -

<div class="card" *ngIf="commentsFormEnable">
    <div class="card">
      <h3 class="mb-0">
        <button class="btn  btn-primary btn-sm" aria-expanded="false">
          Comments
        </button>
      </h3>
      <form [formGroup]="commentsForm" data-target="comments" id="commentsForm" (ngSubmit)="onSubmit($event)">
        <div class="row">
          <div class="col">
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">Comments</span>
              </div>
              <textarea formControlName="comment" class="form-control" [disableControl]="comments" placeholder="Add a comment."></textarea>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col text-right">
            <ng-container *ngIf="comments; else commentsbtnElseBlock">
              <ng-container *ngTemplateOutlet="params;context:{btnId: 'comments', toggleValue: comments}"></ng-container>
            </ng-container>
            <ng-template #commentsbtnElseBlock>
              <ng-container *ngTemplateOutlet="paramsElse;context:{btnId: 'comments', toggleValue: comments}"></ng-container>
            </ng-template>
          </div>
        </div>
        <div class="row">
            <div class="col">
                <div *ngFor="let commentData of commentsList; let i=index">
                  <div class="comment-list">
                    <div class="user-initial border rounded-circle">
                      {{commentData.UpdatedBy | slice:0:1}}
                    </div>
                    <div class="comment-info">
                      <ul>
                        <li class="ml-2 mr-2">
                          <span class="user-name">{{commentData.UpdatedBy}}</span>
                        </li>
                        <li>
                          <span>
                            <small>commented {{commentData.UpdatedDate | date:'medium'}}</small>
                          </span>
                       <!--   <i input type="checkbox" class="fas fa-trash-alt" id="{{commentData.RecordId}}" (change)=onChange($event) (click)="deleteCommentsEntity(i,project.ProjectId)"></i>-->
                        </li>
                      </ul>
                    </div>
                    <div class="ml-5 mb-5">
                      {{commentData.Comment}}
                      <!--<div class="project-desc" style="text-align:left; width:3080px">
                        <app-inline-edit [(ngModel)]=commentData.Comment label=commentData.Comment [required]="true" type="input" style="width:300px">
                        </app-inline-edit>
                      </div>-->
                      <button class="btn btn-pill btn-primary btn-sm mr-2" style="text-align:right;color:Tomato;margin-right:20px;float:right" type="button">
                        <i ng-mouseover="count = count + 1" ng-init="count=0" ng-show="Delete" id="{{commentData.RecordId}}" class="fas fa-trash-alt" style="text-align:right;" (click)="deleteCommentsEntity(commentData.RecordId,project.ProjectId)"></i>
                      </button>
                    </div>
                   </div>
                </div>
              </div>
        </div>
      </form>
    </div>
</div>

My concern here is that the above code works with below packages: -

"@angular/animations": "^8.0.2",
"@angular/common": "^8.0.2",
"@angular/compiler": "^8.0.2",
"@angular/core": "^8.0.2",
"@angular/forms": "^8.0.2",
"@angular/http": "^8.0.0-beta.10",
"@angular/platform-browser": "^8.0.2",
"@angular/platform-browser-dynamic": "^8.0.2",
"@angular/platform-server": "^8.2.11",
"@angular/router": "^8.0.2",

And not working with: -

"@angular/animations": "^9.0.0-rc.12",
"@angular/common": "^9.0.0-rc.12",
"@angular/compiler": "^9.0.0-rc.12",
"@angular/core": "^9.0.0-rc.12",
"@angular/forms": "^9.0.0-rc.12",
"@angular/platform-browser": "^9.0.0-rc.12",
"@angular/platform-browser-dynamic": "^9.0.0-rc.12",
"@angular/platform-server": "^8.2.11",
"@angular/router": "^9.0.0-rc.12",

I am getting this error -

cannot read property 'disabled' of undefined at DisableControlDirective.set cannot read property 'disable' of undefined

Could it be a problem with the version upgrade? I am clueless. Please suggest if there is any way to resolve this issue.


回答1:


For some reason angular 9 injected ngControl is not working as it's was working in angular 8, As a Workaround you can wrap this.ngControl.controlaction inside setTimeout

Try this:

import { NgControl } from '@angular/forms';
import { Directive, Input } from '@angular/core';

@Directive({
    selector: '[disableControl]'
})
export class DisableControlDirective {
        @Input('appDisable') set disableControl( condition : boolean ) {
            const action = condition ? 'disable' : 'enable';
            setTimeout(()=>{
              this.ngControl.control[action]();
            });
       }
    constructor (private ngControl : NgControl){}
}

Or

You can fix this issue by listening binding change inside ngOnChanges like this

 @Input('appDisable') disableControl;
   ngOnChanges(changes) {
    if (changes['disableControl']) {
      const action = this.opDisabled ? 'disable' : 'enable';
      this.ngControl.control[action]();
    }
  }

For More Information check this



来源:https://stackoverflow.com/questions/60075561/directives-not-working-with-formcontrol-after-upgrading-to-angular-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!