Using FormcontrolName with label in angular 6
up vote
0
down vote
favorite
I have angular 6 project with reactive forms. I have a grid like below. Detail button, open a modal window and showing student infos. My html of dialog is like below. But it gave me No value accessor for form control with name: studentNumber error. Is my html logic is wrong?
This not working
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label formControlName="studentNumber"></label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label formControlName="studentAge"></label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label formControlName="studentType"></label>
</div>
</p-dialog>
</form>
When I tried like below, working perfectly. But, this is very long for writing everywhere.
<label>studentForm.controls.studentNumber.value</label>
When I tried like below, working perfectly
<input formControlName="studentNumber">

angular typescript
add a comment |
up vote
0
down vote
favorite
I have angular 6 project with reactive forms. I have a grid like below. Detail button, open a modal window and showing student infos. My html of dialog is like below. But it gave me No value accessor for form control with name: studentNumber error. Is my html logic is wrong?
This not working
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label formControlName="studentNumber"></label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label formControlName="studentAge"></label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label formControlName="studentType"></label>
</div>
</p-dialog>
</form>
When I tried like below, working perfectly. But, this is very long for writing everywhere.
<label>studentForm.controls.studentNumber.value</label>
When I tried like below, working perfectly
<input formControlName="studentNumber">

angular typescript
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have angular 6 project with reactive forms. I have a grid like below. Detail button, open a modal window and showing student infos. My html of dialog is like below. But it gave me No value accessor for form control with name: studentNumber error. Is my html logic is wrong?
This not working
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label formControlName="studentNumber"></label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label formControlName="studentAge"></label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label formControlName="studentType"></label>
</div>
</p-dialog>
</form>
When I tried like below, working perfectly. But, this is very long for writing everywhere.
<label>studentForm.controls.studentNumber.value</label>
When I tried like below, working perfectly
<input formControlName="studentNumber">

angular typescript
I have angular 6 project with reactive forms. I have a grid like below. Detail button, open a modal window and showing student infos. My html of dialog is like below. But it gave me No value accessor for form control with name: studentNumber error. Is my html logic is wrong?
This not working
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label formControlName="studentNumber"></label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label formControlName="studentAge"></label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label formControlName="studentType"></label>
</div>
</p-dialog>
</form>
When I tried like below, working perfectly. But, this is very long for writing everywhere.
<label>studentForm.controls.studentNumber.value</label>
When I tried like below, working perfectly
<input formControlName="studentNumber">

angular typescript
angular typescript
asked Nov 11 at 12:08
Hasan Ozdemir
25312
25312
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
To not repeat yourself I would create simple directive for that case:
import Directive, HostBinding, Optional from '@angular/core';
import ControlContainer from '@angular/forms';
@Directive(
selector: 'label[controlName]',
)
export class LabelControl
@Input() controlName: string;
constructor(@Optional() private parent: ControlContainer)
@HostBinding('textContent')
get controlValue()
return this.parent ? this.parent.control.get(this.controlName).value : '';
And use it as follows:
<label controlName="studentNumber"></label>
Ng-run Example
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
I create your directive. But it didn't worked @yurzui. It gave meCannot read property 'value' of null
– Hasan Ozdemir
Nov 11 at 13:04
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
1
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
|
show 2 more comments
up vote
0
down vote
IN component, you can create a getter method which will return form control value.
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label>getControlLabel('studentNumber')</label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label>getControlLabel('studentAge')</label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label>getControlLabel('studentType')</label>
</div>
</p-dialog>
</form>
Component:
getControlLabel(type: string)
return studentForm.controls[type].value;
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
1
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
1
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
1
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
|
show 3 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
To not repeat yourself I would create simple directive for that case:
import Directive, HostBinding, Optional from '@angular/core';
import ControlContainer from '@angular/forms';
@Directive(
selector: 'label[controlName]',
)
export class LabelControl
@Input() controlName: string;
constructor(@Optional() private parent: ControlContainer)
@HostBinding('textContent')
get controlValue()
return this.parent ? this.parent.control.get(this.controlName).value : '';
And use it as follows:
<label controlName="studentNumber"></label>
Ng-run Example
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
I create your directive. But it didn't worked @yurzui. It gave meCannot read property 'value' of null
– Hasan Ozdemir
Nov 11 at 13:04
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
1
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
|
show 2 more comments
up vote
3
down vote
accepted
To not repeat yourself I would create simple directive for that case:
import Directive, HostBinding, Optional from '@angular/core';
import ControlContainer from '@angular/forms';
@Directive(
selector: 'label[controlName]',
)
export class LabelControl
@Input() controlName: string;
constructor(@Optional() private parent: ControlContainer)
@HostBinding('textContent')
get controlValue()
return this.parent ? this.parent.control.get(this.controlName).value : '';
And use it as follows:
<label controlName="studentNumber"></label>
Ng-run Example
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
I create your directive. But it didn't worked @yurzui. It gave meCannot read property 'value' of null
– Hasan Ozdemir
Nov 11 at 13:04
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
1
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
|
show 2 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
To not repeat yourself I would create simple directive for that case:
import Directive, HostBinding, Optional from '@angular/core';
import ControlContainer from '@angular/forms';
@Directive(
selector: 'label[controlName]',
)
export class LabelControl
@Input() controlName: string;
constructor(@Optional() private parent: ControlContainer)
@HostBinding('textContent')
get controlValue()
return this.parent ? this.parent.control.get(this.controlName).value : '';
And use it as follows:
<label controlName="studentNumber"></label>
Ng-run Example
To not repeat yourself I would create simple directive for that case:
import Directive, HostBinding, Optional from '@angular/core';
import ControlContainer from '@angular/forms';
@Directive(
selector: 'label[controlName]',
)
export class LabelControl
@Input() controlName: string;
constructor(@Optional() private parent: ControlContainer)
@HostBinding('textContent')
get controlValue()
return this.parent ? this.parent.control.get(this.controlName).value : '';
And use it as follows:
<label controlName="studentNumber"></label>
Ng-run Example
answered Nov 11 at 12:39
yurzui
91.8k10180204
91.8k10180204
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
I create your directive. But it didn't worked @yurzui. It gave meCannot read property 'value' of null
– Hasan Ozdemir
Nov 11 at 13:04
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
1
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
|
show 2 more comments
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
I create your directive. But it didn't worked @yurzui. It gave meCannot read property 'value' of null
– Hasan Ozdemir
Nov 11 at 13:04
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
1
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
This is what I was looking for. Thanks @yurzui
– Hasan Ozdemir
Nov 11 at 12:46
I create your directive. But it didn't worked @yurzui. It gave me
Cannot read property 'value' of null– Hasan Ozdemir
Nov 11 at 13:04
I create your directive. But it didn't worked @yurzui. It gave me
Cannot read property 'value' of null– Hasan Ozdemir
Nov 11 at 13:04
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Can you provide simple demo on stackblitz?
– yurzui
Nov 11 at 13:05
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
Sorry, my mistake. I did misspelling (lowerCase letter). Your code worked perfectly. Thanks again @yurzui
– Hasan Ozdemir
Nov 11 at 13:08
1
1
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
If we rename it to formControlName then Angular built-in directive will break your code because you will have to implement custom ControlValueAccessor. But if you want more code is executed in your application then you can definitely do this. Here's an example ng-run.com/edit/xSPCiA4IjYvLXuIQGUY6
– yurzui
Nov 11 at 13:24
|
show 2 more comments
up vote
0
down vote
IN component, you can create a getter method which will return form control value.
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label>getControlLabel('studentNumber')</label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label>getControlLabel('studentAge')</label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label>getControlLabel('studentType')</label>
</div>
</p-dialog>
</form>
Component:
getControlLabel(type: string)
return studentForm.controls[type].value;
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
1
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
1
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
1
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
|
show 3 more comments
up vote
0
down vote
IN component, you can create a getter method which will return form control value.
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label>getControlLabel('studentNumber')</label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label>getControlLabel('studentAge')</label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label>getControlLabel('studentType')</label>
</div>
</p-dialog>
</form>
Component:
getControlLabel(type: string)
return studentForm.controls[type].value;
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
1
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
1
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
1
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
|
show 3 more comments
up vote
0
down vote
up vote
0
down vote
IN component, you can create a getter method which will return form control value.
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label>getControlLabel('studentNumber')</label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label>getControlLabel('studentAge')</label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label>getControlLabel('studentType')</label>
</div>
</p-dialog>
</form>
Component:
getControlLabel(type: string)
return studentForm.controls[type].value;
IN component, you can create a getter method which will return form control value.
<form [formGroup]="studentForm">
<p-dialog header="Student Detail" [(visible)]="displayStudentForm">
<div class="p-grid">
<label>Student Number</label>
<label>getControlLabel('studentNumber')</label>
</div>
<div class="p-grid">
<label>Student Age</label>
<label>getControlLabel('studentAge')</label>
</div>
<div class="p-grid">
<label>Student Type</label>
<label>getControlLabel('studentType')</label>
</div>
</p-dialog>
</form>
Component:
getControlLabel(type: string)
return studentForm.controls[type].value;
edited Nov 11 at 12:28
answered Nov 11 at 12:17
Suresh Kumar Ariya
4,0551215
4,0551215
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
1
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
1
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
1
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
|
show 3 more comments
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
1
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
1
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
1
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Thanks for your reply @SureshKumarAriya. But in real scenario, there will be 30 label not 3. And foreach, I will write get method. I don't know, Is this proper and recomended way? Because, I'm looking recomended way for this?
– Hasan Ozdemir
Nov 11 at 12:20
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
Or should I use template driven forms for doing this @SureshKumarAriya ?
– Hasan Ozdemir
Nov 11 at 12:26
1
1
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
Updated the code. is it fine?
– Suresh Kumar Ariya
Nov 11 at 12:29
1
1
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
You can follow the first approach (i.e getter method) if we have around 10 form control labels. In your case its 30 right. Its suggessted way to call component method which will return form control value based on input value. I have used this in many places in our application and its a recommended one. Template Driven Form approach is like Angular JS approach, we won't prefer that as Angular 2+ is modern web framework.
– Suresh Kumar Ariya
Nov 11 at 12:36
1
1
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
Yes, you can use directive also. Its a good option
– Suresh Kumar Ariya
Nov 11 at 12:43
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248582%2fusing-formcontrolname-with-label-in-angular-6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown