- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这里有一些代码。同样的模式(afaik)适用于英雄教程。
login.component.html:
<div class="four wide column middle aligned" *ngIf="wrongCredentialsInserted">
<div class="ui error message">Invalid credentials
</div>
</div>
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,我已经验证了这一点。我也尝试过
*ngIf="wrongCredentialsInserted === true"
,因为我在其他地方读过,但它没有用。
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;
}
}
<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>
@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;
}
}
最佳答案
*ngIf
有几个可能的原因对模型的变化没有反应。
更改检测未运行
您正在使用 OnPush
您的组件上的策略并更改组件状态,而无需手动触发 CD 循环。要么重新打开自动 CD,要么通过注入(inject) ChangeDetectorRef
手动触发 CD。并使用适合您需要的方法。
风格会误导你
有可能ngIf
绑定(bind)工作正常,并且模板被正确创建和销毁,但是有些样式在视觉上会掩盖这一点,例如 display: none
, visibility: hidden
等。确保通过打开浏览器的开发工具检查页面。
有一个 Uncaught Error
如果您在开发时没有打开控制台,很容易错过。可能有一个错误破坏了你的代码,Angular 无法从中恢复;从而防止任何进一步的 CD 循环运行和更新 DOM。确保有一个打开的控制台来检查错误。
你甚至没有改变模型
Angular 是一个框架,您可以在其中声明性地指定您希望如何基于模型创建 DOM。您可以通过编写模板来做到这一点。如果你的模型没有改变,DOM 也不会改变。确保正确的代码段正在运行。一个快速的方法是放置一个 console.log
在更改变量的代码附近。您还可以在浏览器的开发工具中放置断点,或使用 Chrome 的实用程序扩展,例如 Augury独立于基于模板呈现的方式检查模型。
关于angular - *ngIf 不对 bool 值变化使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46388831/
我有一个带有列的表提供者 implied(tiny int)(something like nullable bool) provi
我正在阅读 VideoFileWriter来自 AForge.Video.FFMPEG 的类(class)通过 ILSPY 组装(我很想看看特定方法是如何工作的)并发现了这个: public bool
这是我的完整代码... import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import
我有一个输入 list类型 [Maybe SomeType]和一个谓词 p类型 SomeType -> Bool ,我想回答这个问题“谓词 p 是否适用于所有碰巧在输入中的 SomeType ?”。
使用 !!x 有什么区别吗?对比(bool)x ? 假设__STDC_VERSION__ >= 199901L和 #include 他们都保证结果是0吗?或 1 ,并且无论 x 的大小和值如何,都不
我正在编写一些 C++ 代码,我想调用两个函数(checkXDirty 和 checkYDirty),并返回 true如果任一返回 true。即使一个返回 true 我也需要评估两者,所以我的第一个想
我注意到 bool在 QtCreator 中以不同于其他类型的颜色突出显示: 只有在包含某些 header 时才会发生这种情况,最终我将其追踪到 . QtCreator 的代码检查器似乎无法手动跟踪
有一个函数: func (first: Int) -> Int -> Bool -> String { return ? } 返回值怎么写?我对上面 func 的返回类型感到很困惑。 最
训练神经网络学习“异或” 我正在尝试使用“批量归一化”,我创建了一个批量归一化层函数“batch_norm1”。 import tensorflow as tf import nump
我已经创建了任务函数来验证我的 json 文件。一切正常,直到我没有使用结果。当我试图从 async task function 获得结果时它显示错误为 Cannot implicitly conve
我有一个函数 func login (parameters: [(String, Any)], completion: @escaping (Bool) -> Vo
我正在处理最近从 X/Motif 转移到 Qt 的 C++ 代码库。我正在尝试编写一个 Perl 脚本,它将用 bool 替换所有出现的 Boolean(来自 X)。该脚本只是做了一个简单的替换。 s
嗨,我正尝试创建一个Visiblity小部件,如果用户在Firebase数据库阵列上,该小部件将显示。看起来像这样(成员数组): 如您所见,我创建了一个StreamBuilder,如果当前用户的用户名
我创建了如下的rest api方法, Future activateAccount(int id, int code) async{ final body = {"code": '$c
在我的Flutter应用中,我有一个返回Future的函数,但我想将结果作为Stream。这是函数: Future isGpsOn() async { if (await Geolocat
我可以看到 BOOLEAN 覆盖了 __visit_name__ class BOOLEAN(Boolean): __visit_name__ = 'BOOLEAN' 控制调度员选择的访问者方
考虑以下代码: bool x; bool? y = null; x = y?? true; 将 bool? 分配给 bool 是一个编译时错误,但上面的代码在编译和运行时都成功了。为什么?尽管第三条语
我正在重写一些 Javascript 代码以在 Excel VBA 中工作。由于在这个网站上搜索,我已经设法翻译了几乎所有的 Javascript 代码!但是,有些代码我无法准确理解它在做什么。这是一
我想拍一张bool来自Vec并在 if 语句中进行比较。如何解决以下错误? | 7 | if cell { | ^^^^ expected
我在我的应用程序崩溃跟踪工具中发现了一些崩溃。基本上我有一个 tabBarController,其中一个选项卡有一个嵌入式 UIWebView,另一个选项卡有一个带有 UITableView 的 Co
我是一名优秀的程序员,十分优秀!