【发布时间】:2018-03-05 10:57:26
【问题描述】:
这里有一些代码。同样的模式 (afaik) 适用于英雄教程。
login.component.html:
<div class="four wide column middle aligned" *ngIf="wrongCredentialsInserted">
<div class="ui error message">Invalid credentials
</div>
</div>
login.component.ts:
wrongCredentialsInserted: boolean = false;
//...
onSubmit(login: LoginDto): void {
console.log(`User '${login.username}' attempts to login...`);
if (this.authService.login(login.username, login.password)) {
this.location.back();
} else {
this.wrongCredentialsInserted = true; //This line gets executed!
}
}
即使我将wrongCredentialsInserted 设置为true,也不会显示该消息。它设置为 true,我已经验证了这一点。我也尝试过*ngIf="wrongCredentialsInserted === true" 之类的东西,因为我在其他地方读到过,但没有用。
我读到这可能与“从 Angular 2 开始的单向数据流”有关,但我知道我们能够在我们公司的 A2+ 项目中做这些事情。 AFAIK 一种数据绑定方式仅指组件间通信。
非常感谢任何形式的帮助。
编辑:由于我所做的事情似乎有点混乱,我将整个文件发布在这里。
login.component.ts
import {AbstractControl, FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {routerTransition} from '../../router.transition';
import {Component} from '@angular/core';
import {AuthService} from '../auth.service';
import {LoginDto} from './login-dto';
import {Location} from '@angular/common';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
animations: [routerTransition()]
})
export class LoginComponent {
private readonly USERNAME: string = 'username';
private readonly PASSWORD: string = 'password';
myForm: FormGroup;
username: AbstractControl;
password: AbstractControl;
message: string;
wrongCredentialsInserted = false;
constructor(public fb: FormBuilder,
public authService: AuthService,
public location: Location) {
this.message = '';
this.myForm = fb.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
this.username = this.myForm.controls[this.USERNAME];
this.password = this.myForm.controls[this.PASSWORD];
}
onSubmit(login: LoginDto): void {
console.log(`User '${login.username}' attempts to login...`);
if (this.authService.login(login.username, login.password)) {
this.location.back();
} else {
this.wrongCredentialsInserted = true;
}
}
login(username: string, password: string): boolean {
this.message = '';
if (!this.authService.login(username, password)) {
this.message = 'Incorrect credentials.';
setTimeout(
function () {
this.message = '';
}.bind(this), 2500);
}
return false;
}
logout(): boolean {
this.authService.logout();
return false;
}
}
login.component.html
<div class="ui raised segment">
<form [formGroup]="myForm"
(ngSubmit)="onSubmit(myForm.value)"
class="ui form"
[class.error]="!myForm.valid">
<div class="field"
[class.error]="!username.valid && username.touched">
<label for="username">Username:</label>
<input type="text"
id="username"
placeholder="Username"
[formControl]="username">
<div *ngIf="username.hasError('required') && username.touched"
class="ui error message">
Username is required
</div>
</div>
<div class="field"
[class.error]="!password.valid && username.touched">
<label for="password">Password:</label>
<input type="text"
id="password"
placeholder="Password"
[formControl]="password">
<div *ngIf="password.hasError('required') && password.touched"
class="ui error message">Password is required
</div>
</div>
<div class="ui grid">
<div class="two wide column middle aligned">
<button type="submit"
class="ui button"
[class.disabled]="!myForm.valid">Submit
</button>
</div>
<div class="fourteen wide column middle aligned" *ngIf="wrongCredentialsInserted">
<div
class="ui error message">Invalid credentials
</div>
</div>
</div>
</form>
</div>
login.component.css:空
auth.service.ts:
@Injectable()
export class AuthService {
constructor(private http: Http) {
}
login(user: string, password: string): boolean {
if (user === 'user' && password === 'password') {
localStorage.setItem('username', user);
return true;
}
return false;
}
logout(): any {
localStorage.removeItem('username');
}
getUser(): any {
return localStorage.getItem('username');
}
isLoggedIn(): boolean {
console.log(`Is user logged in? ' + ${this.getUser() !== null}`);
return this.getUser() !== null;
}
}
【问题讨论】:
-
它对我有用。你能提供更多的代码吗?
-
打开浏览器的调试控制台(在大多数浏览器中通常是 F12 键)。您可能在其他地方遇到了模板错误(与此无关),这可能导致模板代码停止工作。
-
检查 css 的不透明度:0;显示:无;可见性:隐藏...
-
试试 *ngIf="wrongCredentialsInserted == true",它应该可以工作
-
@AlejandroCamba 为什么你认为
*ngIf="wrongCredentialsInserted == true"应该可以工作,而*ngIf="wrongCredentialsInserted"和*ngIf="wrongCredentialsInserted === true"不行?
标签: angular angular2-directives angular-directive