首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular2 :不能绑定到“formGroup”,因为它不是“form”的已知属性

Angular2 :不能绑定到“formGroup”,因为它不是“form”的已知属性
EN

Stack Overflow用户
提问于 2017-04-06 07:46:04
回答 7查看 124.4K关注 0票数 33

我刚开始学角形2,我试着做一种反应式,但我遇到了一些麻烦。在堆栈中搜索了很多次之后,我没有找到解决方案。

在这里你可以看到我的错误

守则:

意见:

代码语言:javascript
运行
复制
    <main>
        <div class="container">
            <h2>User data</h2>
            <form [formGroup]="userForm">
                <div class="form-group">
                    <label>name</label>
                    <input type="text" class="form-control" formControlName="name">
                </div>
                <div class="form-group">
                    <label>email</label>
                    <input type="text" class="form-control" formControlName="email">
                </div>
                <div class="" formGroupName="adresse">
                    <div class="form-group">
                        <label>Rue</label>
                        <input type="text" class="form-control" formControlName="rue">
                    </div>
                    <div class="form-group">
                        <label>Ville</label>
                        <input type="text" class="form-control" formControlName="ville">
                    </div>
                    <div class="form-group">
                        <label>Cp</label>
                        <input type="text" class="form-control" formControlName="cp">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </main>

我的module.ts

代码语言:javascript
运行
复制
    import { NgModule }      from '@angular/core';
    import { CommonModule }  from '@angular/common';
    import { BrowserModule } from '@angular/platform-browser';
    import { ContactComponent } from './contact.component';
    import { FormGroup , FormControl , ReactiveFormsModule , FormsModule } from '@angular/forms';
    
    
    @NgModule({
      imports: [
        NgModule,
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        FormGroup,
        FormControl
      ],
      declarations: [
        ContactComponent
      ]
    })
    export class ContactModule {}



And my component.ts

    import {Component} from '@angular/core';
    import { FormGroup , FormControl } from '@angular/forms';
    
    @Component({
      selector: 'contact',
      templateUrl: './contact.component.html'
      // styleUrls: ['../../app.component.css']
    })
    export class ContactComponent {
    
        userForm = new FormGroup({
            name: new FormControl(),
            email: new FormControl(),
            adresse: new FormGroup({
                rue: new FormControl(),
                ville: new FormControl(),
                cp: new FormControl(),
            })
        });
    }

我不明白我的错误,因为导入ReactiveForm在这里。所以..。我需要你的帮助:)谢谢

在我阅读了您的答案并重构了我的代码之后,没关系,这是有效的!问题是我在页面联系人的模块中导入了反应性模块,我需要在我的应用程序模块中导入这个模块。因此,非常感谢您的兴趣:)

希望这个答案能帮助其他人。

EN

Stack Overflow用户

发布于 2018-12-17 18:00:09

我通过在FormModule中导入shared.module并在所有其他模块中导入shared.module来解决这个问题。我的例子是在多个模块中使用FormModule。

票数 0
EN
查看全部 7 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43248849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档