- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的表单,假设显示错误消息
errorm-messages.component
import { Component, OnInit, Input } from '@angular/core'
import { NgIf } from '@angular/common'
import {REACTIVE_FORM_DIRECTIVES, FormGroup } from '@angular/forms'
@Component(
{
moduleId: module.id,
selector: 'epimss-error-messages',
template: `
<span class="error" *ngIf="errorMessage !== null">{{errorMessage}}</span>`,
styles: [],
directives: [REACTIVE_FORM_DIRECTIVES, NgIf]
})
export class ErrorMessagesComponent implements OnInit {
@Input()
ctrlName: string
constructor(private _form: FormGroup) {
}
ngOnInit() {
}
get errorMessage() {
// Find the control in the Host (Parent) form
let ctrl = this._form.find(this.ctrlName);
console.log('ctrl| ', ctrl)
// for (let propertyName of ctrl.errors) {
// // If control has a error
// if (ctrl.errors.hasOwnProperty(propertyName) && ctrl.touched) {
// // Return the appropriate error message from the Validation Service
// return CustomValidators.getValidatorErrorMessage(propertyName);
// }
// }
return null;
}
<div layout = "column"
layout-align = "start start">
<md-input required
placeholder = "First"
formControlName = "first"></md-input>
<epimss-error-messages ctrlName = "first"></epimss-error-messages>
<md-input placeholder = "Second"
formControlName = "second"></md-input>
<!--<epimss-error-messages ctrlName = "second"></epimss-error-messages>-->
</div>
<epimss-error-messages ctrlName = "first"></epimss-error-messages>
EXCEPTION: Error in http://localhost:4200/app/registration/language/language.component.html:44:2 browser_adapter.ts:74 EXCEPTION: Error in http://localhost:4200/app/registration/language/language.component.html:44:2BrowserDomAdapter.logError @ browser_adapter.ts:74BrowserDomAdapter.logGroup @ browser_adapter.ts:85ExceptionHandler.call @ exception_handler.ts:50(anonymous function) @ application_ref.ts:396ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216NgZoneImpl.runInner @ ng_zone_impl.ts:96NgZone.run @ ng_zone.ts:211ApplicationRef_.run @ application_ref.ts:384ApplicationRef_.bootstrap @ application_ref.ts:408(anonymous function) @ application_ref.ts:150ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356onInvokeTask @ ng_zone_impl.ts:53ZoneDelegate.invokeTask @ zone.js:355Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 browser_adapter.ts:74 ORIGINAL EXCEPTION: No provider for FormGroup!BrowserDomAdapter.logError @ browser_adapter.ts:74ExceptionHandler.call @ exception_handler.ts:62(anonymous function) @ application_ref.ts:396ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216NgZoneImpl.runInner @ ng_zone_impl.ts:96NgZone.run @ ng_zone.ts:211ApplicationRef_.run @ application_ref.ts:384ApplicationRef_.bootstrap @ application_ref.ts:408(anonymous function) @ application_ref.ts:150ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356onInvokeTask @ ng_zone_impl.ts:53ZoneDelegate.invokeTask @ zone.js:355Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 browser_adapter.ts:74 ORIGINAL STACKTRACE:BrowserDomAdapter.logError @ browser_adapter.ts:74ExceptionHandler.call @ exception_handler.ts:66(anonymous function) @ application_ref.ts:396ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216NgZoneImpl.runInner @ ng_zone_impl.ts:96NgZone.run @ ng_zone.ts:211ApplicationRef_.run @ application_ref.ts:384ApplicationRef_.bootstrap @ application_ref.ts:408(anonymous function) @ application_ref.ts:150ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356onInvokeTask @ ng_zone_impl.ts:53ZoneDelegate.invokeTask @ zone.js:355Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 browser_adapter.ts:74 Error: DI Exception at NoProviderError.BaseException [as constructor] (exceptions.ts:13) at NoProviderError.AbstractProviderError [as constructor] (reflective_exceptions.ts:51) at new NoProviderError (reflective_exceptions.ts:84) at ReflectiveInjector_._throwOrNull (reflective_injector.ts:839) at ReflectiveInjector_._getByKeyDefault (reflective_injector.ts:868) at ReflectiveInjector_._getByKey (reflective_injector.ts:830) at ReflectiveInjector_.get (reflective_injector.ts:623) at ElementInjector.get (element_injector.ts:15) at ElementInjector.get (element_injector.ts:15) at _View_LanguageComponent0.createInternal (LanguageComponent.template.js:217)BrowserDomAdapter.logError @ browser_adapter.ts:74ExceptionHandler.call @ exception_handler.ts:67(anonymous function) @ application_ref.ts:396ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216NgZoneImpl.runInner @ ng_zone_impl.ts:96NgZone.run @ ng_zone.ts:211ApplicationRef_.run @ application_ref.ts:384ApplicationRef_.bootstrap @ application_ref.ts:408(anonymous function) @ application_ref.ts:150ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356onInvokeTask @ ng_zone_impl.ts:53ZoneDelegate.invokeTask @ zone.js:355Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 browser_adapter.ts:74 ERROR CONTEXT:BrowserDomAdapter.logError @ browser_adapter.ts:74ExceptionHandler.call @ exception_handler.ts:71(anonymous function) @ application_ref.ts:396ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216NgZoneImpl.runInner @ ng_zone_impl.ts:96NgZone.run @ ng_zone.ts:211ApplicationRef_.run @ application_ref.ts:384ApplicationRef_.bootstrap @ application_ref.ts:408(anonymous function) @ application_ref.ts:150ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356onInvokeTask @ ng_zone_impl.ts:53ZoneDelegate.invokeTask @ zone.js:355Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 browser_adapter.ts:74 DebugContext {_view: _View_LanguageComponent0, _nodeIndex: 30, _tplRow: 44, _tplCol: 2}BrowserDomAdapter.logError @ browser_adapter.ts:74ExceptionHandler.call @ exception_handler.ts:72(anonymous function) @ application_ref.ts:396ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216NgZoneImpl.runInner @ ng_zone_impl.ts:96NgZone.run @ ng_zone.ts:211ApplicationRef_.run @ application_ref.ts:384ApplicationRef_.bootstrap @ application_ref.ts:408(anonymous function) @ application_ref.ts:150ZoneDelegate.invoke @ zone.js:323onInvoke @ ng_zone_impl.ts:64ZoneDelegate.invoke @ zone.js:322Zone.run @ zone.js:216(anonymous function) @ zone.js:571ZoneDelegate.invokeTask @ zone.js:356onInvokeTask @ ng_zone_impl.ts:53ZoneDelegate.invokeTask @ zone.js:355Zone.runTask @ zone.js:256drainMicroTaskQueue @ zone.js:474ZoneTask.invoke @ zone.js:426 browser_adapter.ts:84 EXCEPTION: Error in http://localhost:4200/app/registration/language/language.component.html:44:2 ...
最佳答案
不需要注入(inject)FormGroup,直接作为类使用即可。
所以你的构造函数应该只是
constructor() {
this._form = new FormGroup({
first: new FormControl('value')
})
}
关于angular - 原始异常 : No provider for FormGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38539632/
我有一个父组件,它是主要的 FormGroup .在此 组件我有一个子组件, . 子组件有一个 *ngIf="some-conditional" ,在子组件内部,我有一个快速填充按钮,它基本上是 pa
我找到了这个答案,它展示了如何将表单控件添加到父表单: Reuse components in angular2 model driven forms 但我希望能够像这样将新的 formGroup 添
我在 Plunkr 上有一个 Angular 2 RC4 基本表单示例,它似乎抛出以下错误(在 Chrome DEV 控制台中) 这是笨蛋 https://plnkr.co/edit/GtPDxw?p
这个问题在这里已经有了答案: How to use formControlName and deal with nested formGroup? (4 个答案) 关闭 3 年前。 我有这个用于 a
我正在尝试将 formGroup 添加到我的整体表单中,条件是在我的情况下特定字段属于某种类型。我添加此 formGroup 的函数如下所示: initSocial() { const
情况: 我正在尝试在我的 Ionic 2 应用程序中创建一个非常简单的登录表单。 无论我尝试什么,我都会不断收到此错误: formGroup expects a FormGroup instance.
在我的 Angular 2 组件中,我使用带有 18 个 AbstractControl 的 Formgroup。用户可以编辑从数据库加载的个人资料。我想添加一个 Reset 按钮来将表单重置为其原始
我是 Angular 2 的新手,无法解决这个问题即使在阅读了其他堆栈溢出答案之后也是如此。 我刚刚开始学习 Angular react 形式,想尝试第一个例子,但被卡住了。请帮忙。 这是 HTML
我在 Angular 4 中创建了一个 formGroup,其中用户和组织存储在对象中。现在我想使用这两个对象填充我的表单组。在我的 ts 中,我做了以下事情: createForm(user: an
我了解错误的根源,我正在测试的组件需要将 FormGroup 传递到它的 @Input() 表单:FormGroup。我只是不知道如何在测试此组件时传入一个。 当我调用 fixture.detectC
我不确定这是否是一个错误,几乎 90% 肯定不是,但我想知道发生这种情况背后的逻辑。 我有一个组件,假设我在组件初始化时初始化一个 FormGroup。 export class FooCompone
我有一个定义 FormGroup 的 Angular 组件,它有一个嵌套的 FormGroup 作为它的控件之一。 子 FormGroup 作为 @Input 参数传递给子 component,并且在
我正在 Angular 2 中创建一个表单。我的目标是从 API 获取数据并将其传递到表单中以进行编辑。但是,我遇到了这个错误: EXCEPTION: Uncaught (in promise): E
我有第二种形式。我需要填写来自银行的日期字段,一个 json 文件。在 populateFields 方法中,我得到一个包含将要填充的数据的参数。但是,它只填充itens的值,即数组内部的描述,它不执
选择验证时是否有任何首选方式 myForm.controls['name'].valid myForm.get('name').valid 因为两者似乎只是在句法上有所不同,但实现了相同的目标。 Na
我在使用 Angular 2 表单时遇到了一些问题,我在我的项目中成功地实现了一些表单,当我尝试添加这个表单时,我的 Angular Cli 出现了错误:类型“FormGroup”不可分配给类型“ty
我的情况很简单。有一个包含两个字段的 html 表单: 名字 姓氏 我希望在用户更改 firstName 时更改 lastName。我通过 Angular (8) 中可观察到的 valueChange
我有以下模板。我正在尝试掌握响应式表单,但遇到了问题。 First N
我目前正在使用 Angular 6 创建一个带有 Reactive Form 的 Accordion。我也在使用 primeNG 的 Accordion 模块。 基于对象(cuList),将动态创建F
在 StackOverflow 的其他示例中,有很多关于在 FormArrays 中使用 FormGroups 的问题。但我的问题恰恰相反。 FormArrays 有一个 push 方法,这使得很多事
我是一名优秀的程序员,十分优秀!