Angular2 User input rc.5, No access attribute to control form with unspecified name

I have a simple user input component like

import {Component, Provider, forwardRef} from "@angular/core"; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms"; const noop = () => {}; const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CustomInputComponent), multi: true }; @Component({ selector: 'custom-input', template: ` <input class="form-control" [(ngModel)]="value" name="somename" (blur)="onTouched()"> `, providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR] }) export class CustomInputComponent implements ControlValueAccessor{ //The internal data model private _value: any = ''; //Placeholders for the callbacks private _onTouchedCallback: () => void = noop; private _onChangeCallback: (_:any) => void = noop; //get accessor get value(): any { return this._value; }; //set accessor including call the onchange callback set value(v: any) { if (v !== this._value) { this._value = v; this._onChangeCallback(v); } } //Set touched on blur onTouched(){ this._onTouchedCallback(); } //From ControlValueAccessor interface writeValue(value: any) { this._value = value; } //From ControlValueAccessor interface registerOnChange(fn: any) { this._onChangeCallback = fn; } //From ControlValueAccessor interface registerOnTouched(fn: any) { this._onTouchedCallback = fn; } } 

and I have an application module like this,

 /** * Created by amare on 8/15/16. */ import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { ReactiveFormsModule, FormsModule } from '@angular/forms'; import { AppComponent } from './app/app.component'; import {CustomInputComponent} from "./app/shared/custom.input.component"; import {RouterModule} from "@angular/router"; @NgModule({ imports: [ BrowserModule, ReactiveFormsModule, FormsModule, RouterModule ], declarations: [ AppComponent, CustomInputComponent], bootstrap: [ AppComponent ] }) export class AppModule { } 

and main

 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app.module'; platformBrowserDynamic().bootstrapModule(AppModule); 

and I used my user input in one of my components, as shown below, but I get "No access accessor to control the form with an undefined name attribute".

 <custom-input name="firstName" [(ngModel)]="firstName"></custom-input> 

and app.component is as follows

 import { Component } from '@angular/core'; @Component({ moduleId: module.id, selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.css'] }) export class AppComponent { title = 'app works!'; firstName: string; } 
+64
angular
Aug 15 '16 at 15:45
source share
3 answers

Adding ngDefaultControl to the user input component in the host solves the problem thanks to @danieleds

+60
Aug 20 '16 at 11:13
source share

Add ngDefaultControl to the user input component. This adds a two-way data binding, you won’t need to implement data access methods unless you are doing something unique.

 <custom-input name="firstName" [(ngModel)]="firstName" ngDefaultControl></custom-input> 
+38
Aug 30 '16 at 19:04
source share

Add ngDefaultControl to your input. eg,

 <inline-editor type="textarea" [(ngModel)]="editableTextArea" (onSave)="saveEditable($event)" value="valor" ngDefaultControl> </inline-editor> 

Then import { FORM_DIRECTIVES } from '@angular/common' ;

Finally, the directives: [FORM_DIRECTIVES]

This will work :) Thanks for the comments above.

+8
Aug 24 '16 at 10:55
source share



All Articles