javascript - Angular2: Bind form context to ngTemplateOutlet -


i'm trying define component containing dynamic form (using reactiveforms) user should able add / delete controls. controls can take many forms, , has defined outside of component, think templateref appropriate this.

i'm struggling find way bind externally defined control internal form, through use of formcontrolname

here start of implementation:

// expandable.component.ts  [...] @component({   selector: 'expandable',   templateurl: 'app/component/common/expandable/expandable.component.html',   styleurls: ['app/component/common/expandable/expandable.component.css'] }) export class expandablecomponent {   @contentchild('childtemplate') childtemplate: templateref<any>;    @input() children: array<any>;   public form: formgroup;    constructor(private _changedetector: changedetectorref,               private _formbuilder: formbuilder) {     this.children = [];   }    public ngoninit(): void {     this.form = this._formbuilder.group({       children: this._formbuilder.array([])     });      const arraycontrol = <formarray>this.form.controls['children'];     this.children.foreach(child => {       const group = this.initchildform();       arraycontrol.push(group);     });   }    private initchildform(): abstractcontrol {     return this._formbuilder.group({       key: ['initial key', [validators.required]],       value: ['initial value', [validators.required]]     });   }    public addchild(): void {     const control = <formarray>this.form.controls['children'];     control.push(this.initchildform());     this._changedetector.detectchanges();   } } 

-

<!-- expandable.component.html -->  <form [formgroup]="form">   <div class="form-group">     <div formarrayname="children">       <div *ngfor="let child of form.controls.children.controls; let i=index">         <div [formgroupname]="i">           <template               [ngtemplateoutlet]="childtemplate"               [ngoutletcontext]="{ $implicit: child }"></template>         </div>       </div>     </div>   </div>   <a (click)="addchild()">add child</a> </form> 

attempt define the template externally:

  <expandable>     <template #childtemplate>       <input class="form-control"          formcontrolname="value" />       </template>   </expandable> 

i'm naively trying bind formcontrolname implicitly passed context outter , no luck, i'm getting "cannot find control name: 'value'". ideally, able formcontrolname binding expandable.component.html instead, see no way of doing either.

any thoughts this?

yes, possible:

you need implement controlvalueaccessor interface ngmodel , formcontrolname inject, so:

@directive({   selector: 'control',   providers: [     {provide: ng_value_accessor, multi: true, useexisting: switchcontrolcomponent}   ] }) export class switchcontrolcomponent implements controlvalueaccessor {   ison: boolean;   _onchange: (value: any) => void;    writevalue(value: any) {     this.ison = !!value;   }    registeronchange(fn: (value: any) => void) {     this._onchange = fn;   }    registerontouched() {}    toggle(ison: boolean) {     this.ison = ison;     this._onchange(ison);   } } 

html:

  <expandable>     <template #childtemplate>       <div [formgroup]="form">          <input control class="form-control"             formcontrolname="value" />          </template>       </div>   </expandable> 

further reading:

with new forms api, can't add inputs child components without adding additional form tags


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -