Having a complex system of watches and event handlers to try to keep data
in sync is usually too much complexity with very little return value.
On the other hand, we’d like to stay true to the “principle of least
knowledge” (or law of demeter), and only give each section access to the
data it needs.
In other words, we want some way to isolate each of those sections and have
it only operate on a subset of the data provided in the Parent controller.
This is easy to accomplish using a directive. If you have not written
directives before this may sound daunting, but directives of this type are
actually not much more difficult than what you are already doing with
ng-include and a controller.
app.directive('carInformationFor", function(){
return{
templateUrl: 'views/car.html',
controller: 'CarCtrl',
scope: {
car: '=carInformationFor'
}
}
});
You could do this for each one of your sections. Then you could use it like
this assuming parent:controller setup a form object with different objects
attached to it for each section:
<div ng-app="carApp">
<div ng-controller="ParentCtrl">
<div billing-information-for="form.billing"></div>
<div car-information-for="form.car"></div>
<div problem-information-for="form.problem"></div>
<div payment-information-for="form.payment"></div> </div>
If you write your code like this, then each section is perfectly
modularized. All it needs is to be passed in an object to store it’s data
in. You could then easily compose these and re-use them without fear. (For
example you could have another form in your app that would need billing and
payment information, but perhaps it would be for an item other than a car).
Directives are the correct way to write re-usable components and partials
instead of using ng-include.
Hopefully this makes sense, but I would be happy to clarify anything if you
have any questions.
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.