Как выполнить функцию компонента в другом компоненте? - PullRequest
0 голосов
/ 14 декабря 2018

Я использую угловую форму для создания динамических форм.Вот в чем моя проблема с полем поиска автозаполнения Я использую пользовательский тег автозаполнения, и он работает нормально.Так что оба компонента разные.Когда пользователь выбирает опцию в окне поиска автозаполнения, мне нужно выполнить функцию в моем компоненте динамической формы.Как я могу это сделать? Надеюсь, вы не поняли мою проблему.

Мой код:

 export class DynamicFormComponent implements OnInit {
    constructor(private auth: AuthService, private _http: HttpClient, private data: DataShareService, private toastr: ToastrService, private globals:GlobalConstants) {
    }

}




 @Component({
  selector: 'app-form-autocomplete-type',
  template: `
   <!-- <input matInput 
      [matAutocomplete]="auto"
      [formControl]="formControl" 
      [formlyAttributes]="field">
    <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let value of filter | async" [value]="value">
        {{ value }}
      </mat-option>
    </mat-autocomplete>-->

    <input matInput
    [matAutocomplete]="auto"
    [formControl]="formControl"
    [formlyAttributes]="field"
    [placeholder]="to.placeholder">
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayWith">
    <mat-option
      *ngFor="let value of filter | async"
      [value]="value" (onSelectionChange)=updateValues(value)>
      {{ value.userGroupName }}
    </mat-option>
  </mat-autocomplete>
  `,

})
export class AutocompleteTypeComponent extends FieldType  {
  // Optional: only if you want to rely on `MatInput` implementation
  @ViewChild(MatInput) formFieldControl: MatInput;

  filter: Observable<any[]>;


  ngOnInit() {

    this.filter = this.formControl.valueChanges
      .pipe(
        startWith(''),
        switchMap(term => this.to.filter(term)),
      );
  }

  updateValues(val:any){
    //Call service to fetch the details and update other formly fields values
    //update the rank and address formly fields based on the results we fetch from service.It may be single field or a group of fields.
    console.log("Call service to fetch the details and update other formly fields values");
    //this.dynamicFormComponent.getSelectedName(val);

  }
}
...