I am building a large, complex AngularJS application (think ERP system). I am having a hard time deciding when it is appropriate to use ui-view
, ng-include
or a custom directive + templateURL
.
I will give a few concrete examples to give y'all something to work with.
What are the best practices?
Attribute directive also called custom directives are used when no additional template is needed. The directive can execute logic and apply visual changes to the element it is applied to. This is useful if you want to alter the behavior or style of existing HTML-elements, without wrapping them into a new component.
The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.
For any large or complex application, I would suggest organizing as much of your code into re-usable custom directives as possible. Custom directives will allow you to leverage angular directives for maximum re-usability and minimize the repetitive HTML that exists when you rely only on built-in directives.
UI views are appropriate for swapping out controllers and views dynamically depending on the route. If you have application functionality where each view and controller combination is self-contained, then using a ui-view with routes makes sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With