1

I have validation Form using Angular 2 and want to add ng2-select to it

this is my code, Angular & HTML

submit.component.ts  

   . . .

  private city = new FormControl("", Validators.required);
   . . .

  ngOnInit() {
    this.getMelks();

    this.addPropertyForm = this.formBuilder.group({
      . . .
      city: this.city,
     . . .
    });
  }

submit.component.html

 <div class="form-group">
                <input class="form-control" type="text" name="city" [(ngModel)]="melk.city" placeholder="city" min="0" required>
 </div>

the code i want to add :

Angular :

  public city:Array<string> = ['NY','CA' ,'LA'];

HTML :

 <label>City</label>
      <ng-select [allowClear]="false"
                 [items]="city"
                 [disabled]="disabled"
                 (data)="refreshValue($event)"
                 (selected)="selected($event)"
                 (removed)="removed($event)"
                 (typed)="typed($event)"
                 placeholder="Choose the city">
      </ng-select>

Now how can i add ng2-select to my input and the the FormControl?

1
  • what do you want to do? It is not clear how you want to add select to an input... Commented Dec 15, 2016 at 16:24

2 Answers 2

1

One workaround I've used is to create a hidden input right below ng-select and synced it with a ngModel. e.g.

 <label>City</label>
 <ng-select #cityModel
       [allowClear]="false"
       [items]="city"
       [disabled]="disabled"
       (data)="refreshValue($event)"
       (selected)="selected($event)"
       (removed)="removed($event)"
       (typed)="typed($event)"
       placeholder="Choose the city">
  </ng-select>
  <input [hidden]="true" type="text" [(ngModel)]="cityModel" name="cityModel" [formControl]="city">

submit.component.ts

   . . .

  private city = new FormControl("", Validators.required);
  private cityModel;

  . . .

  selected = (selectedCity) => {
      this.cityModel = value;
      this.city.markAsDirty();
  }
  removed = () => {
      this.cityModel = null;
      this.city.markAsDirty();
  }

Although this doesn't help if you do some action on the form control. Like city.disable() because you would be doing that on the hidden element instead.

I also have a PR to address this issue https://github.com/valor-software/ng2-select/pull/758

Sign up to request clarification or add additional context in comments.

Comments

0

try this, hopefully it's compatible with for formBuilder and allows you to grab current value:

<form class="form-group" [formGroup]="cityForm">
                <input class="form-control"formControlName="city" type="text" name="city" [(ngModel)]="melk.city" placeholder="city" min="0" required>



 <label>City</label>
      <ng-select formControlName="citySelect"
 [allowClear]="false"
                 [items]="city"
                 [disabled]="disabled"
                 (data)="refreshValue($event)"
                 (selected)="selected($event)"
                 (removed)="removed($event)"
                 (typed)="typed($event)"
                 placeholder="Choose the city">
      </ng-select>
 </form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.