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{
and I have an application module like this,
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; }
angular
Amare Aug 15 '16 at 15:45 2016-08-15 15:45
source share