question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tag-input doesn't render inside ReactiveForm

See original GitHub issue

I’m using Ionic 2 (but don’t think that should matter). If I put this markup in my template outside of a form element it renders and works correctly:

<tag-input [ngModel]="['aa', 'bb']" [editable]="true" placeholder="test placeholder"></tag-input>

If I put the same code inside a form:

<form novalidate [formGroup]="form">
  <tag-input [ngModel]="['aa', 'bb']" [editable]="true" placeholder="test placeholder"></tag-input>

I get this error when that page renders:

EXCEPTION: 
  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()
});

  Or, if you'd like to avoid registering this form control, indicate that it's standalone in ngModelOptions:

  Example:

  
<div [formGroup]="myGroup">
   <input formControlName="firstName">
   <input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}">
</div>

When I add [ngModelOptions]="{standalone: true}" the input doesn’t render (but no errors). I also tried it with an attribute defined in the FormGroup and using formControlName="myattributenamehere" and it doesn’t render (and no errors).

Do you see what I’m doing wrong or is there an example of using this input element within a reactive form using FormBuilder?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
saschwarzcommented, Mar 14, 2017

In case anyone else has this same issue add theitem-content attribute to the tag-input:

        <ion-item class="tag-input">
            <ion-label stacked>Tags</ion-label>
            <tag-input item-content [ngModel]="tags" formControlName="tags"></tag-input>
        </ion-item>

For my layout I also added class="tag-input" to the wrapping ion-item so I could fix the horizontal alignment of the tag-item via this addition to app.scss:

// fix for horizontal alignment of tag-item inside ion-item
.tag-input .input-wrapper {
   align-items: flex-start;
   tag-input {
     width: 100%;
   }
 }
0reactions
Gbuompriscocommented, Mar 14, 2017

@saschwarz happy to help 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to make dynamic input inside form tag with reactive form
i've faced another problem and the inner formgroup is seperated than the outer group. when change the data in the inner form it's...
Read more >
Introduction to forms in Angular
In reactive forms each form element in the view is directly linked to the form model (a FormControl instance). Updates from the view...
Read more >
FormGroup and FormControl in Angular - LogRocket Blog
In Angular, form controls are classes that can hold both the data values and the validation information of any form element. Every form...
Read more >
Working with Angular 4 Forms: Nesting and Input Validation
Forms in Angular applications can aggregate the state of all inputs that are under that form and provide an overall state like the...
Read more >
Rendering Your Angular Reactive Form Control Value in a ...
<form [formGroup]="form"> <input placeholder="Email" type="email" formControlName="email"> </form> · <p>{{email}}</p> · get email() { return this.form.get('email') ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found