如何在Anngular 5

时间:2018-02-28 22:58:55

标签: javascript angular-material2 angular5 angular-reactive-forms

工作人员好吗?

我需要帮助以下如何在服务中使用反应形式在Angular Material的MatSlected组件中加载表单时设置值。我尝试了几种方法但从未更新屏幕上的值。 有人能帮助我吗?

拥抱

FuncionarioController

export class FuncionarioPopupComponent implements OnInit, OnDestroy {





public funcionarioForm: FormGroup;

editMode:boolean = false;

perfisSubscription : Subscription;

funcionarioSubscription: Subscription;

perfis: Perfil[] = [];

funcionarios: Funcionario[] =[];

funcionarioSelected: any;




  constructor(private perfilService: PerfilService, private funcionarioService:
    FuncionarioService, @Inject(MAT_DIALOG_DATA) public data:
    any, public dialogRef: MatDialogRef<FuncionarioPopupComponent>,
    private fb: FormBuilder) {


    }

  ngOnInit() {
    this.funcionarioSelected = this.data.payload
    this.buildItemForm(this.funcionarioSelected)
    this.loadFuncionariosPage();
    this.initPerfis();


  }

  initCombosValues(){
    if(this.editMode){
      this.funcionarioForm.controls['isam'].setValue(this.funcionarioSelected.isam);
      this.funcionarioForm.controls['gerente'].setValue(this.funcionarioSelected.gerente);

    }

  }



  loadFuncionariosPage() {
    this.funcionarioSubscription = this.funcionarioService.getAllFuncionarios('').subscribe(
      (funcionarios) => {
        this.funcionarios = funcionarios;
        this.initCombosValues();
      });
  }

  initPerfis(): any {
    this.perfisSubscription = this.perfilService.getAllPerfis().subscribe((resp: Response) =>{
      if(resp.data){
        this.perfis = <Perfil[]>resp.data;
      }
    })
  }

  buildItemForm(func: any): any {
    this.editMode = func.id != null || func.id != '' ? true : false;
    this.funcionarioForm = this.fb.group({
      id: [func.id || ''],
      nome: [func.nome || '', [Validators.required]],
      email: [ func.email || '', this.editMode ? [] : [Validators.required, Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]],
      cpf: [func.cpf || '', [Validators.required]],
      matricula:[func.matricula || ''],
      isam: [func.isam || ''],
      gerente: [func.gerente || ''],
      password: [func.password || '', this.editMode ? null : [Validators.required]],
      confirmPassword: [func.confirmPassword || '', this.editMode ? null :[Validators.required]],
      perfis: [this.perfis|| []]
    });
    if(this.editMode){
      this.funcionarioForm.controls['isam'].valueChanges.subscribe(func =>
        this.funcionarioSelected.isam = func);
      this.funcionarioForm.controls['gerente'].valueChanges.subscribe(func =>
        this.funcionarioSelected.gerente = func);
    }

  }
  onChangePerfil(event, perfil: Perfil) {
    perfil.checked = !perfil.checked;
  }


  comparePerfis(prf1: Perfil, prf2: Perfil): boolean{
      return prf1 && prf2 && prf1.id === prf2.id;
  }

  submit() {
    this.dialogRef.close(this.funcionarioForm.value)
  }

  ngOnDestroy(): void {
    if(this.perfisSubscription){
      this.perfisSubscription.unsubscribe();
    }
    if(this.funcionarioSubscription){
      this.funcionarioSubscription.unsubscribe
    }
  }
<div fxFlex="50" class="pr-1">
          <mat-form-field class="full-width">
            <mat-select placeholder="ISAM" formControlName="isam">
              <mat-option>--</mat-option>
              <mat-option *ngFor="let func of funcionarios" [value]="func">
                {{func.nome}}
              </mat-option>
            </mat-select>
          </mat-form-field>
        </div>

      <div fxFlex="50" class="pr-1">
        <mat-form-field class="full-width">
          <mat-select placeholder="Gerente de Conta" formControlName="gerente">
            <mat-option>--</mat-option>
            <mat-option *ngFor="let func of funcionarios" [value]="func">
              {{func.nome}}
            </mat-option>
          </mat-select>
        </mat-form-field>
      </div>

1 个答案:

答案 0 :(得分:0)

我这样解决了:

&#13;
&#13;
buildItemForm(func: any): any {
    this.editMode = func.id != null || func.id != '' ? true : false;
    this.funcionarioForm = this.fb.group({
      id: [func.id || ''],
      nome: [func.nome || '', [Validators.required]],
      email: [ func.email || '', this.editMode ? [] : [Validators.required, Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]],
      cpf: [func.cpf || '', [Validators.required]],
      matricula:[func.matricula || ''],
      isam: [func.isam || ''],
      gerente: [func.gerente || ''],
      password: [func.password || '', this.editMode ? null : [Validators.required]],
      confirmPassword: [func.confirmPassword || '', this.editMode ? null :[Validators.required]],
      perfis: [this.perfis|| []]
    });
    if(this.editMode){
      this.funcionarioForm.controls['isam'].setValue(this.funcionarioSelected.isam, { onlySelf: true });
      this.funcionarioForm.controls['gerente'].setValue(this.funcionarioSelected.gerente, { onlySelf: true });
      this.funcionarioForm.controls['perfis'].setValue(this.funcionarioSelected.perfis, { onlySelf: true })
    }

  }
  
  
  compareById(obj1, obj2) {
    return obj1.id === obj2.id;
  }
&#13;
<mat-form-field class="full-width">
            <mat-select placeholder="ISAM" formControlName="isam" [compareWith]="compareById">
              <mat-option>--</mat-option>
              <mat-option *ngFor="let func of funcionarios" [value]="func">
                {{func.nome}}
              </mat-option>
            </mat-select>
          </mat-form-field>
&#13;
&#13;
&#13;