0

¿Cómo puedo acceder al res.groupId desde otro suscribe?

this.route.params.subscribe(params => {
  this.userId = (params.id) ? params.id : null
  if(this.userId){
    this._userService.view(this.userId).subscribe(res => {
      let users = <User[]>res;
      this.registerForm.controls['client'].setValue(res.clientName);
      this.registerForm.controls['em'].setValue(res.email);
      this.registerForm.controls['pass'].setValue(res.password);
      this.registerForm.controls['ph'].setValue(res.phone);
      this.registerForm.controls['groupId'].setValue(res.groupId);
    });
  }
});

this._operatorgroups.index().subscribe(res => {
  let operatorGroups = <OperatorGroups[]>res;
  this.operatorGroups  = operatorGroups;
  if(this.userId) this.registerForm.controls['groupId'].setValue(res[res.groupId].id);
});

1 respuesta 1

0

Para acceder a valores desde un subscribe diferente es necesario que declares una variable global (o del método) y uses esta:

let groupId; 
this.route.params.subscribe(params => {
  this.userId = (params.id) ? params.id : null
  if(this.userId){
    this._userService.view(this.userId).subscribe(res => {
      let users = <User[]>res;
      this.registerForm.controls['client'].setValue(res.clientName);
      this.registerForm.controls['em'].setValue(res.email);
      this.registerForm.controls['pass'].setValue(res.password);
      this.registerForm.controls['ph'].setValue(res.phone);
      this.registerForm.controls['groupId'].setValue(res.groupId);
      groupId = res.groupId;
    });
  }
});

this._operatorgroups.index().subscribe(res => {
  let operatorGroups = <OperatorGroups[]>res;
  this.operatorGroups  = operatorGroups;
  if(this.userId) this.registerForm.controls['groupId'].setValue(res[groupId].id);
});

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.