Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I namespace my angular directives?

AngularJS defines a ng prefix for its directives like ng-repeat. When I create my own directives is it a best practice to prefix them?

For instance which is better?

<chat></chat>
or
<myown-chat></myown-chat>

Where myown is my website specific namespace.

[UPDATE]

Now the AngularJS guide suggests to namespace directives https://docs.angularjs.org/guide/directive#creating-directives

like image 632
Giorgio Polvara - Gpx Avatar asked Oct 24 '13 09:10

Giorgio Polvara - Gpx


People also ask

Is Angular directive support namespace?

Directives do not support namespaces.

Can we use multiple directives in Angular?

You can not use two structural directives on the same element. You need to wrap your element in another one. It's advised to use ng-container since it wont be rendered in DOM.

Why do we need custom directives in Angular?

A directive is helpful when you have a custom behavior that you want to attach to a DOM element or existing component, and that custom behavior does not require any HTML template.

Can we import directives in Angular?

You can put commonly used directives, pipes, and components into one module and then import just that module wherever you need it in other parts of your application. Notice the following: It imports the CommonModule because the module's component needs common directives.


1 Answers

I do. I think it is a good practice, especially as you use modules from others that may include additional directives, and things get more confused, and create some small chance of name collision.

If your directives are namespaced, then anyone who goes to read or edit the code will know when to look for the directives in the app's own codebase, and not from some 3rd party.

The popular angular-ui project namespaces all their directives, as does angularstrap.

I think of it as a generally adopted standard; a common and good practice.

like image 77
Andrew Kuklewicz Avatar answered Oct 14 '22 10:10

Andrew Kuklewicz