I'm having problems updating the value of a variable within a subscribe to decide whether or not to show a div in the template. The idea is that if the credentials of a user are not correct and the HTTP response of the server is not 200 OK a div with an error message is shown, but I can not get the variable that is responsible for telling me if it is valid or not, "incorrect", change the value. I attach the code:
Component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { Response } from '@angular/http';
import { UsuariosService } from '../../../services/usuarios.service';
import { Usuario } from '../../../interfaces/usuario.interface';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'],
styles: ['
.ng-invalid.ng-touched:not(form){
color: red;
}
']
})
export class LoginComponent implements OnInit{
//Variables
forma: FormGroup;
incorrecto:boolean = true;
constructor(private _usuarioService: UsuariosService, private _router:Router) {
logearse() {
this.datos = {
Nombre: null,
Correo : this.forma.controls['email'].value,
Hash: this.forma.controls['password'].value
}
console.log(this.datos);
this._usuarioService.conectarse(this.datos)
.subscribe( (respuesta:Response) => {
if(respuesta.ok){
this._router.navigate(['/home']);
}else{
this.incorrecto = false
}
});
}
}
Component.html
<div class="modal-dialog cascading-modal mt-5" role="document">
<!--Content-->
<div class="modal-content">
<!--Modal cascading tabs-->
<div class="modal-c-tabs">
<!-- Nav tabs -->
<ul class="nav nav-tabs tabs-2 " role="tablist">
<li class="nav-item purple accent-1" routerLinkActive="active">
<a class="nav-link white-text" [routerLink]="['/login']" role="tab">
<i class="fa fa-user mr-1"></i> Login</a>
</li>
<li class="nav-item purple accent-3" routerLinkActive="active">
<a class="nav-link white-text" data-toggle="tab" [routerLink]="['/signup']" role="tab">
<i class="fa fa-user-plus mr-1"></i> Registro</a>
</li>
</ul>
<div class="tab-content">
<!--Panel 7-->
<div class="tab-pane fade in show active" id="panel7" role="tabpanel">
<!--Formulario-->
<form [formGroup]="forma" (ngSubmit)="logearse()">
<div class="modal-body">
<div class="md-form form-sm mb-5">
<i class="fa fa-envelope prefix"></i>
<input type="email" id="modalLRInput10" class="form-control form-control-sm" placeholder="Introduzca su email" formControlName="email">
<!-- Errores de validación-->
<div *ngIf="forma.controls['email'].errors?.novacio && !forma.controls['email'].pristine" class="form-control-feedback">
El campo no puede estar vacío.
</div>
<div *ngIf="forma.controls['email'].errors?.pattern" class="form-control-feedback">
El formato no es correcto.
</div>
</div>
<div class="md-form form-sm mb-4">
<i class="fa fa-lock prefix"></i>
<input type="password" id="modalLRInput11" class="form-control form-control-sm validate" placeholder="Introduzca su contraseña" formControlName="password">
</div>
<!-- Errores de validación-->
<div *ngIf="forma.controls['password'].errors?.novacio && !forma.controls['password'].pristine" class="form-control-feedback">
El campo no puede estar vacío.
</div>
<!-- PROBLEMA AQUÍ-->
<div class="text-danger form-control-feedback" *ngIf="!incorrecto"> El correo o la contraseña son incorrectos.</div>
<!-- PROBLEMA AQUÍ -->
<div class="text-center mt-2">
<button class="btn orange lighten-2" type="submit" [disabled]="!forma.valid">Log in
<i class="fa fa-sign-in ml-1"></i>
</button>
</div>
</div>
</form>
<!--Footer-->
<div class="modal-footer">
<div class="options text-center text-md-right mt-1">
<p>¿No tiene cuenta?
<a class="purple-text" [routerLink]="['/signup']">Regístrese</a>
</p>
<p>¿Olvidó su
<a href="#" class="purple-text">Contraseña?</a>
</p>
</div>
</div>
</div>
<!--/.Panel 7-->
</div>
</div>
</div>
<!--/.Content-->
</div>
Service
conectarse( datos:Usuario ) :any{
let body = JSON.stringify(datos);
let headers = new Headers({
'Content-Type':'application/json'
});
let url = '${ this.apptime }usuarios/log';
return this.http.post( url, body, { headers })
.map( res => {
if(res.ok){
this.crearSesion(res.text());
}
return res;
})
}