首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Angular2 2:ngModel不能用父formGroup指令注册表单控件

Angular2 2:ngModel不能用父formGroup指令注册表单控件
EN

Stack Overflow用户
提问于 2016-10-12 09:31:50
回答 1查看 9.8K关注 0票数 3

最近,我将我的angular2项目更新为2.0.2版本,当我使用输入标记时,我得到了以下错误:

错误,

代码语言:javascript
运行
复制
Uncaught (in promise): Error: Error in app/articles/articles.html:42:20 caused by: 
  ngModel cannot be used to register form controls with a parent formGroup directive.  Try using
  formGroup's partner directive "formControlName" instead.  Example:


<div [formGroup]="myGroup">
  <input formControlName="firstName">
</div>

In your class:

this.myGroup = new FormGroup({
   firstName: new FormControl()
});

我的模板

代码语言:javascript
运行
复制
<div *ngIf = 'showform'>
  <form class="nobottommargin" [formGroup]="form" (ngSubmit)="onSubmit(form.value)" novalidate="novalidate">

    <div class="form-process"></div>

    <div class="col_full">
        <label for="template-contactform-name">Title</label>
        <input type="text" formControlName="articletitle" placeholder="Title" class="sm-form-control required" aria-required="true">
    </div>

    <div class="clear"></div>

    <div class="col_full">
        <label for="template-contactform-message">Description</label>
        <textarea class="required sm-form-control" formControlName="articledescription" placeholder="Description" rows="6" cols="30" aria-required="true"></textarea>
    </div>
    <div class="clear"></div>

<div class="col_full">
        <label >Tags</label>
         <tag-input [ngModel]="['@item']"
           [autocompleteItems]="['Item1', 'item2', 'item3']"
           [autocomplete]="true">
</tag-input> 
    <div class="col_full">
        <button class="button button-3d nomargin" type="submit" id="template-contactform-submit" name="template-contactform-submit" value="submit">Save</button>
    </div>

  </form>
</div>

我的部件,

代码语言:javascript
运行
复制
this.form = new FormGroup({
               articletitle: new FormControl(''),
               articledescription: new FormControl(''),
               tags: new FormControl('')
    });

ngModule.ts,

代码语言:javascript
运行
复制
    import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Category }  from './categories/category';
import { Login }  from './login/login';
import { Mainapp }  from './components/app.component';
import { Topics }  from './topics/topics';
import { SignUp }  from './signup/signup';
import { Articles }  from './articles/articles';
import { Article }  from './article/article';
import { User }  from './user/user';
import { HttpModule } from '@angular/http';
import { RouterModule }   from '@angular/router';
import { TagInputModule } from 'ng2-tag-input';
import { App }  from './tags/tags';


// import { TagInputModule } from 'ng2-tag-input';
// import {Ng2TagsInputModule} from 'ng2-tagsinput';
@NgModule({
  imports: [ BrowserModule, ReactiveFormsModule,FormsModule,TagInputModule,HttpModule,
      RouterModule.forRoot([

      { path: '', component:Login },
      { path: 'login', component:Login },
      { path: 'signup', component: SignUp },
      { path: 'categories', component: Category },
      { path: 'topics/:id', component: Topics },
      { path: 'articles/:id', component: Articles },
      { path: 'article/:id', component: Article },
      { path: 'user', component: User },
            { path: 'app', component: App },


    ])],
  declarations: [ Mainapp,App,Login,SignUp,Category,Topics,Articles,Article,User ]
  ,bootstrap: [ Mainapp ]
})
export class AppModule { }

我不知道这个错误是怎么回事,有人能给我建议帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2017-08-04 11:17:52

我知道这是个老问题,但今天早上我也面临着同样的问题,终于找到了解决办法,我注意到你错过了比我同样的事情。

您有三个字段/输入(条款标题、项目描述和标签),但只有其中两个字段/输入(标题和项目描述,但没有标签)有一个formControlName指令。

如果您的一个字段缺少formControlName指令,而有一个ngModel属性(这是标记输入的情况),那么您将得到这个错误。

在我的例子中,我遇到的所有字段都有一个ngModel 一个formControlName指令,除了一个只有ngModel的字段。我把它添加到输入中,这样就可以了。

因此,在标记输入中添加formControlName指令,它应该工作得很好。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39995197

复制
相关文章

相似问题

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