Angular 2. Chrome中的错误:找不到具有未指定名称属性的控件

Mik*_*keS 9 google-chrome angular

这个plunker在Firefox中工作,没有任何控制台错误,但在Chrome中我收到消息:

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>
Run Code Online (Sandbox Code Playgroud)

找不到具有未指定名称属性的控件

小智 5

由于您已[formControl]="childControl"在MyChild模板中指定,因此需要在MyChild类中指定FormControl.

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() {}

}
Run Code Online (Sandbox Code Playgroud)

然而,在此之后,您最终会得到一个似乎无关的错误 TypeError: v is not a function