I had the same error, but in my case apparently it was a synchronization issue, at the moment of render the components html.
I followed some of the solutions proposed on this page but any of them worked for me, at least not completely.
What did actually solve my error was to write the below code snippet inside the father html tag of the elements .
I was binding to the variable.
Code:
*ngIf="variable-name"
The error was caused, apparently by the project trying to render the page, apparently at the moment of evaluating the variable, the project just could no find its value. With the above code snippet you make sure that before rendering the page you ask if the variable has being initialized.
This is my component.ts code:
import { Component, OnInit } from '@angular/core';
import { InvitationService } from 'src/app/service/invitation.service';
import { BusinessService } from 'src/app/service/business.service';
import { Invitation } from 'src/app/_models/invitation';
import { forEach } from '@angular/router/src/utils/collection';
@Component({
selector: 'app-invitation-details',
templateUrl: './invitation-details.component.html',
styleUrls: ['./invitation-details.component.scss']
})
export class InvitationDetailsComponent implements OnInit {
invitationsList: any;
currentInvitation: any;
business: any;
invitationId: number;
invitation: Invitation;
constructor(private InvitationService: InvitationService, private BusinessService:
BusinessService) {
this.invitationId = 1; //prueba temporal con invitacion 1
this.getInvitations();
this.getInvitationById(this.invitationId);
}
ngOnInit() {
}
getInvitations() {
this.InvitationService.getAllInvitation().subscribe(result => {
this.invitationsList = result;
console.log(result);
}, error => {
console.log(JSON.stringify(error));
});
}
getInvitationById(invitationId:number){
this.InvitationService.getInvitationById(invitationId).subscribe(result => {
this.currentInvitation = result;
console.log(result);
//this.business=this.currentInvitation['business'];
//console.log(this.currentInvitation['business']);
}, error => {
console.log(JSON.stringify(error));
});
}
...
Here is my html markup:
<div class="container-fluid mt--7">
<div class="row">
<div class="container col-xl-10 col-lg-8">
<div class="card card-stats ">
<div class="card-body container-fluid form-check-inline"
*ngIf="currentInvitation">
<div class="col-4">
...
I hope this can be helpful.
I had the same error, but in my case apparently it was a synchronization issue, at the moment of render the components html.
I followed some of the solutions proposed on this page but any of them worked for me, at least not completely.
What did actually solve my error was to write the below code snippet inside the father html tag of the elements .
I was binding to the variable.
Code:
*ngIf="variable-name"
The error was caused, apparently by the project trying to render the page, apparently at the moment of evaluating the variable, the project just could no find its value. With the above code snippet you make sure that before rendering the page you ask if the variable has being initialized.
This is my component.ts code:
import { Component, OnInit } from '@angular/core';
import { InvitationService } from 'src/app/service/invitation.service';
import { BusinessService } from 'src/app/service/business.service';
import { Invitation } from 'src/app/_models/invitation';
import { forEach } from '@angular/router/src/utils/collection';
@Component({
selector: 'app-invitation-details',
templateUrl: './invitation-details.component.html',
styleUrls: ['./invitation-details.component.scss']
})
export class InvitationDetailsComponent implements OnInit {
invitationsList: any;
currentInvitation: any;
business: any;
invitationId: number;
invitation: Invitation;
constructor(private InvitationService: InvitationService, private BusinessService:
BusinessService) {
this.invitationId = 1; //prueba temporal con invitacion 1
this.getInvitations();
this.getInvitationById(this.invitationId);
}
ngOnInit() {
}
getInvitations() {
this.InvitationService.getAllInvitation().subscribe(result => {
this.invitationsList = result;
console.log(result);
}, error => {
console.log(JSON.stringify(error));
});
}
getInvitationById(invitationId:number){
this.InvitationService.getInvitationById(invitationId).subscribe(result => {
this.currentInvitation = result;
console.log(result);
//this.business=this.currentInvitation['business'];
//console.log(this.currentInvitation['business']);
}, error => {
console.log(JSON.stringify(error));
});
}
...
Here is my html markup:
<div class="container-fluid mt--7">
<div class="row">
<div class="container col-xl-10 col-lg-8">
<div class="card card-stats ">
<div class="card-body container-fluid form-check-inline"
*ngIf="currentInvitation">
<div class="col-4">
...
I hope this can be helpful.
Comments
dylhunn
removed
the
help wanted
An issue that it suitable for a community contributor (based on its complexity/scope).
label
Nov 3, 2022
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 4, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 4, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 4, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 4, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 7, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 7, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 7, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
dylhunn
added a commit
to dylhunn/angular
that referenced
this issue
Nov 7, 2022
…guide. [A Github issue](angular#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
AndrewKushnir
pushed a commit
that referenced
this issue
Nov 8, 2022
…guide. (#47969)
[A Github issue](#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
PR Close #47969
AndrewKushnir
pushed a commit
that referenced
this issue
Nov 8, 2022
…guide. (#47969)
[A Github issue](#43821) about an arcane-sounding Forms error is one of the repo's top-ten most visited pages. This converts the error to `RuntimeErrorCode` and adds a dedicated guide to explain how to solve it.
PR Close #47969
The error “No value accessor for form control with name” occurs when you are programming in the Angular framework. It’s not an impossible exception. With just a few lines of code, you can fix this error.
There are two common reasons cause this problem.
1. When we add the formControlName attribute into the element like the label.
<form [formGroup]="addressForm">
<label for="fname" formControlName="fname">Full name</label>
<input id="fname" type="text" />
<label for="age" formControlName="age">Age</label>
<input id="age" type="number" />
</form>
With the above code sample, we get an error like this:
ERROR Error: No value accessor for form control with name: 'fname'
ERROR Error: No value accessor for form control with name: 'age'
2. We miss the providers of @Component while we use Control Value Accessor.
@Component({
selector: 'my-input',
templateUrl: './my-input.component.html',
styleUrls: ['./my-input.component.scss'],
// Missing providers HERE
})
The above code sample makes an error like this:
ERROR Error: No value accessor for form control with name: 'myFormControl'
Solutions for this error
Case 1: Place the formControlName on an HTML element supported by Angular
You should use formControlName = “fname”
and formControlName = “age”
on an input
tag, not on a label
tag.
Like this:
<form [formGroup]="addressForm">
<label for="fname">Full name</label>
<input id="fname" type="text" formControlName="fname" />
<label for="age">Age</label>
<input id="age" type="number" formControlName="age" />
</form>
Output:
Case 2: Create a provider of the ControlValueAccessor
Implement interface
So, to solve this problem, we have to implement the ControlValueAccessor Interface by implementing three functions of ControlValueAccessor:
writeValue()
(to write to the view when programmatic changes from model to view are requested).registerOnChange()
(creates a function called at the time the control’s value changes in view).registerOnTouched()
(creates a function called at the time the component gets a touch or blur event).
Create a provider
Let Angular know that your component is a ControlValueAccessor. First, you have to register a provider.
The provider should contain NG_VALUE_ACCESSOR
. You’ll also need a forwardRef()
in this provider.
For instance, if your component is named MyComponent, you should create a provider containing the provide is NG_VALUE_ACCESSOR
and pass it into the component:
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => MyComponent),
multi: true,
}
]
Put it together
After implementing the ControlValueAccessor and registering a provider, the source code looks like this:
@Component({
selector: 'my-input',
templateUrl: './my-input.component.html',
styleUrls: ['./my-input.component.scss'],
//Added Providers
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CustomInputComponent),
multi: true,
},
],
})
export class CustomInputComponent implements ControlValueAccessor {
onChange: any = () => {};
onTouch: any = () => {};
registerOnChange(fn: any): void {
this.onChange = fn;
}
registerOnTouched(fn: any): void {
this.onTouch = fn;
}
input: string;
writeValue(input: string) {
this.input = input;
}
}
Result
You can use the formControlName
attribute properly, and your form will work as expected without error.
<h1>My Form</h1>
<form [formGroup]="form">
<my-input formControlName="myFormControl">
</my-input>
</form>
Summary
We have shown you two solutions to fix the “No value accessor for form control with name” error. So just choose the solution that best suits your case. And then you can fix it quickly.
Have a great day.
Maybe you are interested:
- “Cannot Read Property ‘FindIndex’ Of Undefined” In JavaScript – How To Solve It?
- “Element type is invalid, expected a string (for built in components) or a class/function but got” – How to solve?
Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java
I had the same error, but in my case apparently it was a synchronization issue, at the moment of render the components html.
I followed some of the solutions proposed on this page but any of them worked for me, at least not completely.
What did actually solve my error was to write the below code snippet inside the father html tag of the elements .
I was binding to the variable.
Code:
*ngIf="variable-name"
The error was caused, apparently by the project trying to render the page, apparently at the moment of evaluating the variable, the project just could no find its value. With the above code snippet you make sure that before rendering the page you ask if the variable has being initialized.
This is my component.ts code:
import { Component, OnInit } from '@angular/core';
import { InvitationService } from 'src/app/service/invitation.service';
import { BusinessService } from 'src/app/service/business.service';
import { Invitation } from 'src/app/_models/invitation';
import { forEach } from '@angular/router/src/utils/collection';
@Component({
selector: 'app-invitation-details',
templateUrl: './invitation-details.component.html',
styleUrls: ['./invitation-details.component.scss']
})
export class InvitationDetailsComponent implements OnInit {
invitationsList: any;
currentInvitation: any;
business: any;
invitationId: number;
invitation: Invitation;
constructor(private InvitationService: InvitationService, private BusinessService:
BusinessService) {
this.invitationId = 1; //prueba temporal con invitacion 1
this.getInvitations();
this.getInvitationById(this.invitationId);
}
ngOnInit() {
}
getInvitations() {
this.InvitationService.getAllInvitation().subscribe(result => {
this.invitationsList = result;
console.log(result);
}, error => {
console.log(JSON.stringify(error));
});
}
getInvitationById(invitationId:number){
this.InvitationService.getInvitationById(invitationId).subscribe(result => {
this.currentInvitation = result;
console.log(result);
//this.business=this.currentInvitation['business'];
//console.log(this.currentInvitation['business']);
}, error => {
console.log(JSON.stringify(error));
});
}
...
Here is my html markup:
<div class="container-fluid mt--7">
<div class="row">
<div class="container col-xl-10 col-lg-8">
<div class="card card-stats ">
<div class="card-body container-fluid form-check-inline"
*ngIf="currentInvitation">
<div class="col-4">
...
I hope this can be helpful.