1

I have a form that contains different fields : input, select, select multiple, buttons. And I was wondering if it would be a good idea (and actually feasible) to create a component for this form, this component would be like a container that can contains all type of form fields (input, select, button, etc...).

I have put a sample of code on plunker, what I would like to do is to create a component for the form, in which I could insert how many other component I want (button, input, etc..).

<!DOCTYPE html>
<html ng-app="MyApp">

<head>
  <link data-require="[email protected]" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js"></script>
  <script src="script.js"></script>
  <script src="myInput.js"></script>
  <script src="myButton.js"></script>
</head>

<body ng-controller="MyCtrl">
  <div class="container">
    <h2>My form</h2>
    <form role="form">
      <my-input label="Firstname"></my-input>
      <my-input label="Lastname"></my-input>
      <my-button label="Submit"></my-button>
    </form>
  </div>
</body>

</html>
1
  • obviously components are the ideal solution for such cases, especially if they have standalone logic Commented Jun 16, 2016 at 14:16

2 Answers 2

1

If you are planning to reuse the form in other parts of your code, I would say it would be a good idea to create it as a component.

Otherwise, it's just a matter of preference. Sometimes it can be nice to divide large portions of code into different files.

Edit: Plunker example https://plnkr.co/edit/kyt9Q6L01r06dJXLDQZH?p=preview

<body ng-controller="MyCtrl">
  <div class="container">
    <my-form></my-form>
  </div>
</body>

Edit 2: Updated the Plunker with an example of passing objects as attribute to the form

<body ng-controller="MyCtrl">
  <div class="container">
    <my-form inputlabels="inputs1"></my-form>
    <my-form inputlabels="inputs2"></my-form>
  </div>
</body>
Sign up to request clarification or add additional context in comments.

9 Comments

Thanx so you confirm that it is feasible to create a form component that would be like a container of component?
@user3414662 Yes, if you plan on reusing the form or if you don't want to mix the form with your other code. This is a nice approach if you got a really large form with lots of functions and logic bound to it, then it will be easier for you to maintain in the long run if it has it's own files (that is, being used as a component).
Would you have some code to share because I don't know how to make a container component
I added code on plunker, can you please tell me how to put the form in a component in order it to be a container of components
@user3414662 I am not a 100 % what you are trying to do, but I have edited your Plunker as an example of how I would do it. Directives are like components, except they have access to more features. Please refer to this page for differences between components and directives: docs.angularjs.org/guide/component
|
1

If you mark "Yes" to these two questions then you should build a component:

  1. Is it reusable?
  2. Does the form has a unique logic?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.