功能丧失价值

时间:2019-05-20 23:02:41

标签: javascript html angular

我有这个:

@Component({
  selector: 'app-edit',
  templateUrl: './altera-estatutos.component.html',
  styleUrls: ['./altera-estatutos.component.css']
})
export class AlteraEstatutosComponent implements OnInit {

  id: String;
  professor: Utilizador;
  updateForm: FormGroup;
  professores: Utilizador[];
  avaliacaoEditar : any = {};


  constructor(private avalService: AvaliacaoService, private userService: UtilizadorService ,private router: Router, private route: ActivatedRoute, private snackBar: MatSnackBar, private fb: FormBuilder) {
    this.createForm();
  }

  createForm(){
    this.updateForm = this.fb.group({
      docente: ['', Validators.required],
    });
  }


  ngOnInit() {
    this.route.params.subscribe(params => {
      console.log(params);
      this.id = params.id;
      if( this.id != undefined){
        this.userService.getUtilizadoresById(this.id).subscribe( (res: any) => {
        console.log(res);
        this.professor = res;
        this.updateForm.get('docente').setValue(this.professor);
        console.log(this.updateForm.get('docente').value);
        });
      }
    });
    this.userService
    .getUtilizadores()
    .subscribe((data: Utilizador[]) => {
      this.professores = data;
      console.log('Data requested ...');
      console.log("-------------");
      console.log(this.professor);
      console.log("--------------");

    });
    }

    editEstatutos(id){
      this.router.navigate([`/app/altera-estatutos/${id}`]);

    }

这是HTML

<form style="margin-left: 22%"[formGroup]="updateForm" class="edit-form">
  <mat-form-field class="field-full-width">

    <mat-select style="width:500px" placeholder="Docente" formControlName="docente" #docente>
      <mat-option *ngFor="let disc of professores" [value]="disc">
        {{disc.nome}}
      </mat-option>
    </mat-select>

  </mat-form-field><br>      </form>
  <button style="margin-left:40%" mat-raised-button color="primary" (click)="editEstatutos(docente.value._id)">  Procurar Estatutos </button>
  <br><br>
  <span class="cgb">Adicionar </span><span class="cg">Estatuto</span>

这是发生了什么: 运行页面时,我从ID路由收到来自对象的ID。我寻找它并将其放在“教授”中,然后将所有“教授”放入另一个数组中,以显示在页面上。

当我在日志控制台上打印“ this.professores”之类的变量时,就可以了,但是在订阅之外,它们是未定义的。我能做什么?为什么我会丢失所有数据?

1 个答案:

答案 0 :(得分:1)

函数“ subscribe”是异步的,在这种情况下,代码中的函数执行顺序是不同的。

尝试解决方案:

this.route.params.subscribe(params => {
      console.log(params);
      this.id = params.id;
      if( this.id != undefined){
        this.userService.getUtilizadoresById(this.id).subscribe( (res: any) => {
        console.log(res);
        this.professor = res;
        this.updateForm.get('docente').setValue(this.professor);
        console.log(this.updateForm.get('docente').value);
        secondFunction()
        });
      } else {
        secondFunction()
      }
    });

    secondFunction() {
      this.userService
      .getUtilizadores()
      .subscribe((data: Utilizador[]) => {
      this.professores = data;
        console.log('Data requested ...');
        console.log("-------------");
        console.log(this.professor);
        console.log("--------------");
      });
    }