Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2. Error in Chrome: Cannot find control with unspecified name attribute

This plunker work in Firefox with out any console error, but in Chrome I get message:

Error in formcontrol

<ul>
  <li *ngFor="let item of data">
    <label>
      <input type="radio" name="radio1"
        [value]="item.id"
        [formControl]="childControl"
        (input)="fn($event.target.value)" >
      <p>{{ item.title }}</p>
    </label>
  </li>
</ul>

Cannot find control with unspecified name attribute

like image 709
MikeS Avatar asked Feb 26 '17 10:02

MikeS


1 Answers

Since you have [formControl]="childControl" specified in your MyChild template you need a FormControl specified in your MyChild class.

export class MyChild  implements ControlValueAccessor {
  @Input() data: any;
  out: any;
  childControl = new FormControl();

  fn: (value:any) => void;

  validateFn: any = () => {};

  constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}

  writeValue(value: any): void {
    this._renderer.setElementProperty(this._elementRef, 'checked', value == this._elementRef.nativeElement.value);
  }
  registerOnChange(fn: (value: any) => void) {
    this.onChange = fn;
  }
  registerOnTouched() {}

}

However after that you end up with an error that seems to be unrelated TypeError: v is not a function

like image 126
Syed Jafri Avatar answered Nov 04 '22 07:11

Syed Jafri