Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use custom directive vs ui-view vs ng-include in an AngularJS application?

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.

  • A navigation menu that is used across all URLs of the application but includes a complex AJAX autosuggest/dropdown search box.
  • A simple HTML footer that is the same across all URLs of the application
  • The content areas that go in between the header and footer.
  • The individual components that are nestled within the content area (such as and "Edit Profile" form, or user dashboard)
  • Modal dialogs

What are the best practices?

like image 965
Jakobovski Avatar asked Jun 15 '14 23:06

Jakobovski


People also ask

When would you use a custom directive?

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.

Which of the following directives is used to start an AngularJS application?

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.

Which directive do we use to inform AngularJS about the parts controlled by it?

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.


1 Answers

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.

like image 73
pixelbits Avatar answered Sep 27 '22 17:09

pixelbits