0

We are developing our Angular 5 application using Angular material. we have 2 set of 4 similar autocomplete type fields. We have implemented using Material AutoComplete fields.

the first set of 4 fields are used to fetch the customer details. the second set of fields fetches the customer details but the visibility of the second set is optional controlled by a checkbox. The functionality was working fine. But when I implemented force selection by means of tag:MatAutocompleteTrigger subscribe the first set of fields are working fine. Whereas for the second set of fields on load should be invisible only while the checkbox is checked, the second set of fields will become visible.

Due to which there is an error, I am placing the code snippet where the error occurs. I am just showing here for one field altCustomerName with the id altCustomerNameId. I tried to make this id value to true on tag:ngOnInit and set to false on tag:ngAfterViewInit. But that also ends up in error.

HTML (view)

<mat-form-field>
     <input placeholder="Customer Name" type="text" #altCustomerNameId matInput [formControl]="altCustomerNameControl" [(ngModel)]='selectedAltCustomerName' [matAutocomplete]="altCustomerName" required>
     <mat-autocomplete #altCustomerName="matAutocomplete" class='customerDetails'>
        <mat-option (click)="setAltCustomerDetails(altCustomer)" *ngFor="let altCustomer of filteredAltCustomersByName | async" [value]="altCustomer.name">
                    {{ altCustomer.custnum + ' ' + altCustomer.name + ' '+ altCustomer.countryname }}
        </mat-option>
     </mat-autocomplete>
</mat-form-field>                 

Component code: (Inside the class the altCustomerNameControl is declared)enter code here

@ViewChild('altCustomerNameId', {
            read: MatAutocompleteTrigger
})
altCustomerNameTrigger: MatAutocompleteTrigger;
        ..
        ....
        ....
ngAfterViewInit() {
    this._subscribeToClosingActions();
}
ngOnDestroy() {
      ....
   if(this.altCustomerNameSubscription &&
       !this.altCustomerNameSubscription.closed)
    .....
}

this.altCustomerCodeSubscription = this.altCustomerCodeTrigger.panelClosingActions.subscribe(
   event => {
      if (!event || !event.source) {
          this.altCustomerNameControl.setValue('');
          this.altCustomerCodeControl.setValue('');
          this.selectedAltCustomerName = '';
          this.selectedAltCustomerCode = '';
          this.selectedAltCustomerCountry = '';
       }
   },err => this._subscribeToClosingActions(),
      () => this._subscribeToClosingActions()
);

Please let me know if you need any clarifications in the code.

Satendra
  • 6,755
  • 4
  • 26
  • 46
Sundar
  • 655
  • 2
  • 7
  • 13

1 Answers1

0

We have used *ngIf to show or hide the section based on the checkbox selection. Modified the same using [style.display] as in below code solved the above issue.

Old code: (This controls the section visibility)

    <div class="row" **ngIf='optAlternativeCustomer'*>

New code: (Modified the section visibility as below)

    <div class="row" *[style.display]="!optAlternativeCustomer?'none':'flex'"*>
Sundar
  • 655
  • 2
  • 7
  • 13