저는 Angular 2에서 새롭습니다. 저는 angular2 + node js 설정 작업을하고 있습니다. 프로젝트에서 간단한 인증 호출. 내가 원하는 것은 로그인 API가 오류를 반환하면 div 블록을 표시하는 것입니다.HTTP 응답의 토글 요소
다음은 내가 한 것입니다.
LoginComponent
private loginResponse: any;
private submitted: boolean;
private error: boolean = false;
private errorMessage: string;
public loginForm: FormGroup;
ngOnInit() {
// if user is authenticated, redirect him/her to dashboard
if (this.auth.isAuthenticated()) {
this.router.navigate(['dashboard'])
}
this.submitted = false;
}
// authenticate user
getLogin() {
// return if form is invalid
if (!this.loginForm.valid) {
return false;
}
this.submitted = true;
var formData = this.loginForm.value;
this.auth.login(formData.email, formData.password, this.translate.currentLang)
.subscribe(
(data: Response) => {
this.loginResponse = data;
if (this.loginResponse.result) {
localStorage.setItem('token', this.loginResponse.data);
window.location.reload();
}
this.submitted = false;
this.error = true;
this.errorMessage = this.loginResponse.message;
},
err => {
console.log(err);
}
);
}
LoginComponent.html
<form [formGroup]="loginForm" id="sign_in" (ngSubmit)="getLogin()" autocomplete="false">
<div *ngIf="error" class="text-danger text-justify msg">
{{ errorMessage }}
</div>
문제, 서버 false를 반환 될 때, 나는 참으로 오류 속성을 설정 해요하지만 어떤 효과를 표시하지 않습니다 .
제안 추가 태그 "angular2" – shaochuancs