FormArray without FormControls and just strings Angular 2 -


i have 2 string arrays on backend have fill either single string or multiple strings. not key value pairs. trying push string 1 of arrays running fact not , cannot specify control: value pair.

my formarray looks

collections: new formarray([]), 

my html select strings

              <md-select placeholder="collection">             <md-option (click)="addcollectionid('one')" value="local">local</md-option>             <md-option (click)="addcollectionid('two')" value="music">music</md-option>             <md-option (click)="addcollectionid('three')" value="performing arts">performing arts</md-option>             <md-option (click)="addcollectionid('four')" value="sports">sports</md-option>             <md-option (click)="addcollectionid('five')" value="restaurants">restaurants</md-option>           </md-select> 

and logic add strings formarray looks like:

  addcollectionid(id: string) { const control = <formarray>this.createcardform.controls['collections']; control.push(id); 

}

i getting error 'argument of type 'string' not assignable parameter of type 'abstractcontrol'.

since cannot push control: value pair , string/strings how can push strings array while still staying in overall form?

any help/tips/suggestions appreciated.

you can use reactive forms api achieve this, recommend use angular formbuilder:

export class selectoverviewexample {   createcardform: formgroup;    foods = [     {value: 'steak-0', viewvalue: 'steak'},     {value: 'pizza-1', viewvalue: 'pizza'},     {value: 'tacos-2', viewvalue: 'tacos'}   ];    // inject form builder   constructor(private fb: formbuilder) {     // add collections form array form     this.createcardform = this.fb.group({       collections: this.fb.array([]),     });   }    // function pushed new value collections array   addcollectionid(val) {     const collections = this.createcardform.get('collections');     // add once     if (!collections.value.includes(val)) {       collections.push(this.fb.control(val));     }   } } 

this way selected values added form , available under createcardform.value.collections array.

here html:

<md-select placeholder="favorite food">   <md-option [disabled]="createcardform.value.collections.includes(food.value)"               *ngfor="let food of foods"               [value]="food.value"               (click)="addcollectionid(food.value)">     {{ food.viewvalue }}   </md-option> </md-select>  <pre>{{ createcardform.value | json }}</pre> 

here updated plunker forked https://material.angular.io/components/select/overview

update

here reactive form solution, without call addcollectionid() function. add reactivecollections field form group:

constructor(private fb: formbuilder) {   this.createcardform = this.fb.group({     collections: this.fb.array([]),     reactivecollections: null   }); } 

add form group , control names md-select:

<form [formgroup]="createcardform">   <md-select placeholder="favorite food"               multiple="true"               formcontrolname="reactivecollections">     <md-option *ngfor="let food of foods" [value]="food.value">       {{ food.viewvalue }}     </md-option>   </md-select> </form> 

plunker updated well


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -