angular - Angular Forms,ng-valid 始终存在
问题描述
在角形教程中,使用“ng-valid”来指示组件中的名称文本框何时不为空。
(教程:https ://angular.io/guide/forms )
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control ng-untouched ng-pristine ng-valid" id="name"
required
[(ngModel)]="model.name" name="name">
</div>
我们添加一个 forms.css 文件,内容如下:
.ng-valid[required], .ng-valid.required {
border-left: 5px solid #42A948; /* green */
}
.ng-invalid:not(form) {
border-left: 5px solid #a94442; /* red */
}
以及应用程序的 index.js 文件中的引用:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularHeroForm</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="assets/forms.css">
</head>
<body>
<app-root></app-root>
</body>
</html>
根据教程,当擦除“name”的内容时,ng-valid 应该消失并ng-invalid
出现在它的位置。
我所看到的是ng-valid
保留并ng-invalid
只是添加到类名的末尾。
此行为导致 angular 无法从文件中应用正确的(红色)样式,forms.css
因为 ng-valid 仍在类名中。
What did I do wrong and why ng-valid
is not disappearing after erasing the name ?
解决方案
The ng-valid/ng-invalid are classes that Angular adds automatically, and there is no need to add them manually.
If you remove the classes that you added to the input, then your sample will work without any problem.
<input type="text" id="name" required [(ngModel)]="model.name" name="name">
推荐阅读
- java - Mesibo 消息列表标题显示数字但我想要用户名
- google-chrome-extension - Chrome 扩展程序:无法解压缩扩展程序
- json - 在PowerShell中解析json的麻烦
- reactjs - Redux 存储正在更新,但组件未重新渲染
- ansible - with_nested 不能按预期在数组中循环
- google-sheets - 谷歌表格公式有时不适用于合并单元格
- ios - watchOS only App – Info.plist 中的隐私使用说明
- android - 如何在颤动中处理音频文件或其他资产的本地化?
- firefox - 一个嵌入的 youtube 视频不播放,其他的作品
- javascript - 如何创建包含非重复数字的数组?