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
Directives do not support namespaces.
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.
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.
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.
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.
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