1

Starting out with angular, Ive developed an app which builds a form with directives dynamically chosen from the field type in the data from the server. All the fields are bound to data within $scope using ng-model. The data is loaded and saved to the server using $http.

Today I've looked at Angular forms, and by wrapping all this in a form element, and giving names to each input, I automatically have validation etc, which is very nice.

But now I notice there are two sets of data maintained - my bound data from my model in $scope, and the data separately maintained by the Angular form directive.

Looking at the Angular docs for eg form or input, the example also binds to $scope (ng-model="text"), but form data is separate (myForm.input.*).

Is this apparent duplication best practice? Or is there a way to unify my model with the data maintained by the Angular form, under one structure?

1 Answer 1

1

The form and ng-model (using ngmodelcontroller) directives provide a mechanism to support form validation and 2 way data binding on input elements.

What the form provides as data is current state of the form and its element, which beyond the standard model that you bind using ng-model. Duplication of code is bad, but here data in the form is being tracked by the form directive so it is fine.

Sign up to request clarification or add additional context in comments.

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.