0

how can i update my variables values on ts function, i have to make click 2 times on the botton that execute the function logreg() to make it work, the first time i make click the comparison "if(this.pass_comp == this.pass ){" is always false no matters that i have already change the the variable pass. y try using ngzone but it didnt work or maybe i use it wrong. some help please

export class LoginComponent implements OnInit {
  pass_comp: string = "";
  pass: string = "";
  user: string = "";
  .
  .
  .

  constructor(private elementRef: ElementRef, private route: Router, private share: LogcontrlService, private api: BackeService) {
    this.elementRef.nativeElement.ownerDocument
      .body.style.backgroundImage = 'url(/assets/imgs/wall.jpg)';
    this.elementRef.nativeElement.ownerDocument
      .body.style.overflow = 'hidden';
  }

  ngOnInit(): void {}


  logreg() {
    if (this.user != "" && this.pass != "") {
      this.api.get_user(this.user).subscribe(x => {
        console.log(x.mensaje);
        if (x.mensaje == "consultado") {
          console.log(x.datos[0]);
          this.pass_comp = x.datos[0].pass;
          this.num = 1;
        }
      });

      console.log(this.pass_comp == this.pass);
      if (this.pass_comp == this.pass) {
        .
        .
        .
      }
    }
  }

1 Answers1

0

Your this.pass_comp == this.pass is outside of the subscription, so when you click the button for the 1st time it's fired before the sub returns a value for pass and the pass_comp is at that moment still an empty string.

Now, even if it does look like it's working on second click as it should, it's not: that IF fires again before the pass is returned by sub, so it compares the previously returned pass with pass from the input.

So, move the IF check inside the subscription handling block.

Misha Mashina
  • 1,739
  • 2
  • 4
  • 10