I would somewhat disagree. If this is your own code and you don't need to make it flexible enough to extend without adding more cases this is fine. Its in no way a great solution nor one I would recommend but the alternatives are much more complex. I really hate promoting my own projects especially when they are no where near what I want them to be but implementing some type of plugin system where each of those controls can be swapped out with another so long as they implement the same interface seems like it would work well. I have not used formly but have read the docs. I think formly is a great solution for building dynamic forms.
This is my project where I had to implement a plugin system basically for the same reason. I started with a bunch of hard coded switch cases but soon realized the limitation. So I migrated to tokens. Realized tokens are not a great solution because services that have already been instantiated don't get any updates. So decided to implement my own plugin service that can not only be extended dynamically but discover plugins from any possibly any location. Including locations outside my source code. Inspired by Drupal. https://github.com/ng-druid/platform/tree/master/modules/plugin/src/lib On Wednesday, March 10, 2021 at 5:43:12 AM UTC-5 bastien...@gmail.com wrote: > It seems you're creating some sort of dynamic form (display date picker if > data is of type date ...) > We have been using either ngx/formly or some angular dynamic component > creation mechanism: https://angular.io/api/core/ComponentFactory > > Hope this helps > > *Bastien Lemaire* > > > On Wed, 10 Mar 2021 at 11:39, john.mb...@gmail.com <john.mb...@gmail.com> > wrote: > >> hi, as you can see in my code below, I am repeating code. I was just >> wondering what the best way would be to refactor this code: >> >> ``` >> <div class="my-2" *ngSwitchCase="'BPFDateTimeElement'"> >> <core-date >> [control]="form.get(section.objectID).get(rowElement.objectID)" >> [element]="rowElement" >> ></core-date> >> </div> >> <div class="my-2" *ngSwitchCase="'BPFBooleanElement'"> >> <core-boolean >> [control]="form.get(section.objectID).get(rowElement.objectID)" >> [element]="rowElement" >> ></core-boolean> >> </div> >> <div class="my-2" *ngSwitchCase="'BPFEntryElement'"> >> <core-input >> [control]="form.get(section.objectID).get(rowElement.objectID)" >> [element]="rowElement" >> ></core-input> >> </div> >> <div class="my-2" *ngSwitchCase="'BPFTextViewElement'"> >> <core-textarea >> [control]="form.get(section.objectID).get(rowElement.objectID)" >> [element]="rowElement" >> ></core-textarea> >> </div> >> <div class="my-2" *ngSwitchCase="'BPFPicklistElement'"> >> <core-pick-list >> [control]="form.get(section.objectID).get(rowElement.objectID)" >> [element]="rowElement" >> ></core-pick-list> >> ``` >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Angular and AngularJS discussion" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to angular+u...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/angular/2818053c-c552-4084-a846-9bad28f832c7n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/angular/2818053c-c552-4084-a846-9bad28f832c7n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group. To unsubscribe from this group and stop receiving emails from it, send an email to angular+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/angular/b2918945-252f-49a8-bcff-c28d27fa8b8dn%40googlegroups.com.