Тёмный
No video :(

Reusable Input and Select component with Control Value Accessor in Angular 

Sebastian Persson
Подписаться 2,4 тыс.
Просмотров 11 тыс.
50% 1

Опубликовано:

 

26 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 116   
@invictuz4803
@invictuz4803 2 месяца назад
The most comprehensive resource on CVA, covering custom error component and two custom input components!
@DevBySeb
@DevBySeb Месяц назад
Thanks for the feedback mate 🙏🏼🙏🏼
@Neotrixstdr
@Neotrixstdr 7 месяцев назад
The best video about Control Value Accesor i have never seen. Thanks!
@DevBySeb
@DevBySeb 7 месяцев назад
Thanks for the awesome feedback mate 🙏🏼🙏🏼
@jacobadlerman3067
@jacobadlerman3067 5 месяцев назад
Agreed with you. A lot of stuff across the internet just shows a pretty simple examples or goes into too deep low-level stuff, and it's hard to grasp what's going on. But this guy made it very simple (just need to know that fancy magic injector, NgControl and FormGroupDirective )
@amandaavelino8362
@amandaavelino8362 Год назад
Good idea to create a directive to implement all custom form controls. I'd never thought about it (:
@DevBySeb
@DevBySeb Год назад
Thanks for the feedback. Hope it helps mate! 🙏
@dannisenson
@dannisenson Год назад
oh man this is quality content. You're the best
@DevBySeb
@DevBySeb Год назад
Much appreciated mate! Thank you for watching 🙏🙏
@prasoon2510
@prasoon2510 4 месяца назад
You are amazing, lot of deep information in each video. Can you please make video to generate a form using json??
@DevBySeb
@DevBySeb 3 месяца назад
Thanks for the suggestion mate I will add this to my todo-list 🙏🏼
@prasoon2510
@prasoon2510 3 месяца назад
@@DevBySeb yes please, and please have nested json so that we can create nested form controls
@PeterStritzinger
@PeterStritzinger 6 месяцев назад
Many THX! This was exactly what I was looking for.
@DevBySeb
@DevBySeb 6 месяцев назад
Thanks for watching mate and for the awesome feedback! :)
@user-gp8pt2xd2v
@user-gp8pt2xd2v Год назад
am getting error "Maximum call stack size exceeded". FormControl.setValue,InputfieldComponent.writeValue,onChange
@DevBySeb
@DevBySeb Год назад
Good spot mate. Add the following code in the top of the "writeValue" function: if(control.value === value) return;
@user-gp8pt2xd2v
@user-gp8pt2xd2v Год назад
thanks@@DevBySeb
@jomerverola4880
@jomerverola4880 11 месяцев назад
Thankyou sir quality content. And I already saved my problem with the use of ngValue
@DevBySeb
@DevBySeb 11 месяцев назад
Thank you for the awesome feedback mate 🙏 and thank you for watching!
@vasilemoraru89
@vasilemoraru89 10 месяцев назад
very well explained, with a lot of details.
@DevBySeb
@DevBySeb 10 месяцев назад
Thanks and thank you for watching mate! 🙏🏼🙏🏼
@PeterStritzinger
@PeterStritzinger 6 месяцев назад
Thank you very much for your work!
@DevBySeb
@DevBySeb 6 месяцев назад
Wow thank you so much mate! Much appreciated! 🙏🙏
@mohammedjoubba318
@mohammedjoubba318 4 месяца назад
bro this is amazing thank you so much
@DevBySeb
@DevBySeb 4 месяца назад
Thanks for watching mate, im glad you liked it 🙏🏼🙏🏼
@coding_check9665
@coding_check9665 Год назад
This is so good! Such great content! Please keep going. Also how can we support you?
@DevBySeb
@DevBySeb Год назад
Thanks a lot for watching and for that feedback mate! 🙏 If you want more content like this, please subscribe and make suggestions on things you would like to learn more about 🙏
@owenjaspervargas1408
@owenjaspervargas1408 6 месяцев назад
This is what I'm looking for! Thank you so much!
@DevBySeb
@DevBySeb 6 месяцев назад
Thanks for the feedback mate 🙏🏼🙏🏼
@test-user-123
@test-user-123 8 месяцев назад
public writeValue(outsideValue: T): void { if (this.control) { this.control.setValue(outsideValue); } else { this.control = new FormControl(outsideValue); } } there will be a cyclic change in value... 1. change the value of the external control 2. writeValue is called 3. let's take the first step
@DevBySeb
@DevBySeb 7 месяцев назад
Good spot mate, there is a thread here outlining that issue 🙏🏼
@obinnaezedei856
@obinnaezedei856 7 месяцев назад
Too many magics! You just have to cram a lot of things. Crazy! Worried about the maintainability
@DevBySeb
@DevBySeb 7 месяцев назад
Unit tests are probz what will save you in those cases 😃
@obinnaezedei856
@obinnaezedei856 6 месяцев назад
@@DevBySeb The fact that you ended your comment with a laugh emoji is all the fun😂😂
@pazzuto
@pazzuto 6 месяцев назад
Very interesting video. A few things that will come in handy. The question is on the directive you created to extend from. It looks just like a base class - why dioes it have to be a directive, perhaps for the injection of the form directive?
@DevBySeb
@DevBySeb 6 месяцев назад
Good question mate. Its as you dag for the injection of the form directive 😃
@videoresponse123
@videoresponse123 6 месяцев назад
so this approach basically gets the formControl from the parent component and places it on the nested input. doesn't a ControlValueAccessor allow us to bypass that? what advantage does this have over simply using HTML's native (change) and then calling the CVA's onChange() function with that value? for example, most documentation I've seen has the registerOnTouched function contain a single line "this.onTouched = fn". why is this one so much more involved? just trying to understand. this looks amazing and really in-depth I'm just curious why this approach differs so much from other ControlValueAccessor documentation I've seen
@DevBySeb
@DevBySeb 6 месяцев назад
Good question mate. CVA allows us to use formControl and formControlName on custom components which makes it easier for us to standardise the workflow across multiple form elements. OnTouched is used to indicate control states that User has focused on the input. You can extend the logic to reset the touched state to hide errors when typing and making the errors re-appear onBlur. This way, we can also standardise the behaviour of the inputs, UX wise.
@jacobadlerman3067
@jacobadlerman3067 5 месяцев назад
So underestimated channel, great job though!
@DevBySeb
@DevBySeb 5 месяцев назад
Thanks for the feedback mate! Much appreciated 🙏🏼🙏🏼
@dmitrykruglov332
@dmitrykruglov332 3 месяца назад
I have a problem when trying form.reset() from parent component. Maximum call stack size exceeded. Could you check it out please?
@DevBySeb
@DevBySeb 3 месяца назад
Good spot mate. Add the following code in the top of the "writeValue" function: if(control.value === value) return;
@FabioGomesCG
@FabioGomesCG 2 месяца назад
When i add the component and use a formGroup, when i reset the form with this.form.reset(); the component causes infinit loop in writeValue function in control-value-acessor, can you have this problem ? Edit: i fix this error: in writeValue function (this.control && this.control.value!=value) resolve to me.
@DevBySeb
@DevBySeb 2 месяца назад
Awesome that you found and fixed the issue mate 🔥
@sivanshlmao
@sivanshlmao Год назад
which extension is that? that autocompletes your codes???
@DevBySeb
@DevBySeb Год назад
It is Github Copilot mate 😁
@sivanshlmao
@sivanshlmao Год назад
@@DevBySeb thanks man
@balvinderrayat6489
@balvinderrayat6489 6 месяцев назад
Great details. Absolutely loved it. Just one request, can you update validation-errors.component.ts file for loop as per latest angular version (17)? While rest of the things are working as expected. But I am getting an error "Type 'Record' must have a '[Symbol.iterator]()' method that returns an iterator"
@DevBySeb
@DevBySeb 6 месяцев назад
Thanks for the comment mate. I will add that my todo 🙏🏼
@MSH001
@MSH001 9 месяцев назад
Thanks for this tutorial, can you do another video like this but with signals instead?
@DevBySeb
@DevBySeb 9 месяцев назад
Thanks for watching mate 🙏🏼 I Will add that to my todo list mate 🙏🏼
@SzymonPeka
@SzymonPeka 5 месяцев назад
Using this directive triggers valueChanges on the initial value.
@DevBySeb
@DevBySeb 4 месяца назад
Good spot and thanks for the feedback mate - I will look into this and re-make the video with Angular 17's new syntax.
@dmitrykruglov332
@dmitrykruglov332 3 месяца назад
Hello again) Found out another problem trying to removeControl() in formGroup. I get an error: "here is no formcontrol instance attached to form control element" It happens in directive, registerOnChange method, when doing tap((val) => fn(val)) I m using your approach in my project, but cant get rid of this error. Could you advise how to fix it? Thanks!
@lnpg87
@lnpg87 Год назад
I have a problems when I use patchValue with method writeChange
@DevBySeb
@DevBySeb Год назад
Can you show sample code mate?
@Constantine4j
@Constantine4j Год назад
It is happening for the same reason that the other user had problem with writeValue. @@DevBySeb
@jimmyp2412
@jimmyp2412 Год назад
Great job!
@DevBySeb
@DevBySeb Год назад
Thanks mate!
@sunilchandramn3001
@sunilchandramn3001 Год назад
very well explained. Thanks
@DevBySeb
@DevBySeb Год назад
Thanks for watching and for the feedback mate! :)
@jomerverola4880
@jomerverola4880 11 месяцев назад
If my control value has already have initial value how to make it default show value of select if no value in control then option[0]
@DevBySeb
@DevBySeb 11 месяцев назад
Good question mate. To make it automatically select the initial value, patch the key, which might be the ID of that object. Then you need to set up logic to show the selected value by another key - which shows the name/title of that item in that object. Having a fallback option like option[0] could be handled in many ways, but if you want to ensure that the control also receives that value - you'll need to override the ngOnInit function and call to patch the value there. If the data is retrieved async - it might be more trivial to set up a listener for the "first" change of that object. It's difficult to give the exact answer without knowing what you want the end result to be/turn out like.
@wiliam334
@wiliam334 Год назад
That is amazing mate. Ta
@DevBySeb
@DevBySeb Год назад
Thanks mate! And thank you for watching 🙏
@shuaibhasanakibbs2366
@shuaibhasanakibbs2366 9 месяцев назад
Great video. More like please.
@DevBySeb
@DevBySeb 9 месяцев назад
Thanks and thank you for watching mate! Will do so for sure! 🙏🏼🙏🏼
@elliottgroves7875
@elliottgroves7875 11 месяцев назад
For some reason the errors always show, the inputs are always dirty, even though the page has loaded and they shouldn't be?
@DevBySeb
@DevBySeb 11 месяцев назад
Do you patch the forms value or do you just initially set it up on the constructor mate?
@elliottgroves7875
@elliottgroves7875 11 месяцев назад
@@DevBySeb I was patching after getting data from an api but even if I comment that code out and just setup the form group from the constructor all form fields seem to be touched and dirty for some reason
@FabioGomesCG
@FabioGomesCG 2 месяца назад
Hello my friend, using this components in a form cause the form.pristine = false, do you have this issue ?
@DevBySeb
@DevBySeb Месяц назад
Hello Fabio, I don’t have the same issue mate. It should be marked as pristine 🤔
@FabioGomesCG
@FabioGomesCG Месяц назад
@@DevBySeb are you sure ? I'm using angular 17... i will try to make an example
@ceklinpersson33
@ceklinpersson33 Год назад
Awesome 🎉
@DevBySeb
@DevBySeb Год назад
🙏🙏🙏
@fuatcetinkaya9111
@fuatcetinkaya9111 10 месяцев назад
What should I do if I want the validation process to occur after clicking the submit button?
@DevBySeb
@DevBySeb 10 месяцев назад
Good question. You can create a function that marks all controls as dirty and touched when pressing a certain Button. You can also remove the mark as touched from the directive
@fuatcetinkaya9111
@fuatcetinkaya9111 10 месяцев назад
Yes, I solved the problem by removing the mark as touched from the directive. You are great.
@DevBySeb
@DevBySeb 10 месяцев назад
@@fuatcetinkaya9111 thanks and thank you for watching mate 🙏
@sunnytekiprofile
@sunnytekiprofile 4 месяца назад
Great explanation dude, I am getting error "Maximum call stack size exceeded" while writing the value. Could you please help me out where i can resolve this :)
@DevBySeb
@DevBySeb 4 месяца назад
Good spot mate. Add the following code in the top of the "writeValue" function: if(control.value === value) return;
@sunnytekiprofile
@sunnytekiprofile 4 месяца назад
You mean to say inside write value function where i need to put the above condition then control.setvalue() condition happening ?
@DevBySeb
@DevBySeb 4 месяца назад
@@sunnytekiprofile yes mate in the writeValue fn in the directive mate 🙏🏼
@sunnytekiprofile
@sunnytekiprofile 4 месяца назад
@@DevBySeb But if i do that my select component giving some error called "Cannot read properties of undefined (reading 'value') at SelectComponent.writeValue "
@sunnytekiprofile
@sunnytekiprofile 4 месяца назад
@@DevBySeb If i am doing that i am getting error like this for my select component "Cannot read properties of undefined (reading 'value') at SelectComponent.writeValue "
@adrian333dev
@adrian333dev Год назад
Great!
@DevBySeb
@DevBySeb Год назад
Thanks mate 🙏🙏
@lubezen
@lubezen 7 месяцев назад
Hi Sebastian. Thanks for the great idea, but how do you reset the select?
@lubezen
@lubezen 7 месяцев назад
found suitable solution in the comments below - if(control.value === value) return; - sorry for the spam and thanks again for the great video.
@DevBySeb
@DevBySeb 7 месяцев назад
Hey mate, it depends mate. You can patchValue from the parent or call to reset the value, both are built in functions in reactive forms.
@lubezen
@lubezen 7 месяцев назад
the thing was, it was entering an infinite loop when i try to do whatever from the outside programatically. But the above mentioned fixed the issue :)@@DevBySeb
@lubezen
@lubezen 7 месяцев назад
to be more specific, I was trying to reset the "custom select" on valueChanges in another control of the form, but then it went to infinite loop. this line - if (this.control.value === value) return; in the writeValue method fixed it. @@DevBySeb
@MaddesMat
@MaddesMat Год назад
👏👏
@DevBySeb
@DevBySeb Год назад
🙏🙏🙏
@fazloonfarook7329
@fazloonfarook7329 10 месяцев назад
Thanks for the content, great explanation. Is there a way to use this with 3 or more layers of components for eg:- => => =>
@DevBySeb
@DevBySeb 10 месяцев назад
Thanks for the feedback mate! 🙏🏼 I believe that it should work but you would have to prop drill the control to the child
@fazloonfarook7329
@fazloonfarook7329 10 месяцев назад
@@DevBySeb but having issues getting the formControl to dirty or touched.
@DevBySeb
@DevBySeb 10 месяцев назад
@@fazloonfarook7329 then it might be a broken connection between the controls somewhere mate
@johniweh2356
@johniweh2356 11 месяцев назад
can this be used for file upload too?
@DevBySeb
@DevBySeb 11 месяцев назад
Good question mate. You can use it with file upload! I usually convert the files into base64 and then patch the form control on file change :)
@pazzuto
@pazzuto 6 месяцев назад
I seem to have run into a bug with this code, FYI. I'm using Reactive Forms and all works for the most part. But, I have the need to update the value of the control based on another event, so I call myform.get("someinput")?.setValue("new value") and then my app crashes with stack overflow error. Calling SetValue causes the SetValue in the Directive to be called and on and on until boom
@DevBySeb
@DevBySeb 6 месяцев назад
Thanks for the comment mate, there is a thread in a comment outlining how to resolve that 🙏🏼
@nouchance
@nouchance Год назад
Thanks 🎉🎉!
@DevBySeb
@DevBySeb Год назад
Thank you mate 🙏🙏
@wiliam334
@wiliam334 Месяц назад
Ta
@anutaNYC
@anutaNYC 7 месяцев назад
I have an issue, with this on cancel the errors show up because I have this code @if(control.invalid && control.touched && (control.touched || control.dirty)) { } I need to have messages show up on save button when empty but not on cancel when I touch the control, so I have to have the check if the control was touched, then it throws a message but if I touch and then cancel I see the message first before the modal closed
@DevBySeb
@DevBySeb 7 месяцев назад
You will need to mark the form as untouched/pristine when cancel is triggered and reset the values mate
@anutaNYC
@anutaNYC 7 месяцев назад
@@DevBySeb i appreciate immensely your response, I’ll try this tomorrow and I thought about that as well, that’s a known issue with modals and forms, and I’m going to have a lot of forms with modals. Again thank you so much, will let you know how it’ll turn out.
@anutaNYC
@anutaNYC 7 месяцев назад
@@DevBySeb Nothing worked because onChange in the validator would kick in before cancel event from the component, so I found a solution instead of click on button do mousedown!!! I would hope there is a better way but this worked, I’m will not sure what the code you wrote really for registeronchange does why not do this._onChange = fn, why do subscribe etc?
@DevBySeb
@DevBySeb 7 месяцев назад
@@anutaNYC Thanks for sharing mate 🙏🏼
Далее
I Built a WATERPARK In My House!
26:28
Просмотров 17 млн
Can You Bend This Bar?
01:00
Просмотров 3,9 млн
Control Value Accessor in Angular [Advanced, 2020]
18:46
React for Angular Developers
39:39
Просмотров 4,9 тыс.
How to Make Forms in Angular REUSABLE (Advanced, 2023)
21:10
The Control Value Accessor | Jennifer Wadella
20:21
Просмотров 24 тыс.
I Built a WATERPARK In My House!
26:28
Просмотров 17 млн