admin管理员组文章数量:1336587
I am building a new app using Angular 18.
> ng version
...
Angular CLI: 18.2.12
...
It is a standalone app (with routing), no NgModules and no app.modules.ts
file.
I want to use Reactive Forms.
In my component class I have declared
import { FormControl, FormGroup } from '@angular/forms';
and
public foobarForm = new FormGroup({
foo: new FormControl(''),
bar: new FormControl(''),
});
In my HTML file, I have
<form [formGroup]="foobarForm">
<input type="text" id="foo" name="foo" formControlName="foo" placeholder="Foo" />
<input type="text" id="bar" name="bar" formControlName="bar" placeholder="Bar" />
...
</form>
I get the following errors
Can't bind to 'formGroup' since it isn't a known property of 'form'
and
Can't bind to 'formControl' since it isn't a known property of 'input'.
Looking art the official docs (at /guide/forms/reactive-forms), it looks like I have to import ReactiveFormsModule
in my app.modules.ts
. But I do not have one.
What is the correct way to use Reactive Forms in a standalone app with version > 17 ?
I am building a new app using Angular 18.
> ng version
...
Angular CLI: 18.2.12
...
It is a standalone app (with routing), no NgModules and no app.modules.ts
file.
I want to use Reactive Forms.
In my component class I have declared
import { FormControl, FormGroup } from '@angular/forms';
and
public foobarForm = new FormGroup({
foo: new FormControl(''),
bar: new FormControl(''),
});
In my HTML file, I have
<form [formGroup]="foobarForm">
<input type="text" id="foo" name="foo" formControlName="foo" placeholder="Foo" />
<input type="text" id="bar" name="bar" formControlName="bar" placeholder="Bar" />
...
</form>
I get the following errors
Can't bind to 'formGroup' since it isn't a known property of 'form'
and
Can't bind to 'formControl' since it isn't a known property of 'input'.
Looking art the official docs (at https://angular.dev/guide/forms/reactive-forms), it looks like I have to import ReactiveFormsModule
in my app.modules.ts
. But I do not have one.
What is the correct way to use Reactive Forms in a standalone app with version > 17 ?
Share Improve this question edited Nov 19, 2024 at 17:20 Naren Murali 59k5 gold badges44 silver badges77 bronze badges asked Nov 19, 2024 at 17:09 VihungVihung 13.4k18 gold badges67 silver badges96 bronze badges1 Answer
Reset to default 3You also have to import the ReactiveFormsModule
in the imports array of the standalone component. The module contains the directives required for reactive forms to work like FormGroupDirective
, FormControlDirective
, which correspond to the attributes [formGroup]
and [formControl]
in the HTML.
We cannot directly import these directives since they are not standalone.
...
import { ReactiveFormsModule } from '@angular/forms';
...
...
@Component({
selector: 'app-root',
template: `
<form [formGroup]="foobarForm">
<input type="text" id="foo" name="foo" [formControl]="foo" placeholder="Foo" />
<input type="text" id="bar" name="bar" [formControl]="bar" placeholder="Bar" />
...
</form>
`,
imports: [ReactiveFormsModule],
})
export class AppComponent {
....
}
Stackblitz Demo
本文标签: How do I use Reactive Forms in Angular 18 without NgModulesStack Overflow
版权声明:本文标题:How do I use Reactive Forms in Angular 18 without NgModules - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742410673a2469723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论