forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Best Practices
Dean Sofer edited this page Aug 30, 2013
·
22 revisions
-
Namespace distributed code
This means usingbs-orui-or something. You could use your initials if you want but you may find it becomes more meaningful if you prefix code by relevant grouping breakdowns (such asloc-for location services ori18n-for translation) -
Only use
.$broadcast(),.$emit()and.$on()for atomic events
This means events that are relevant globally across the entire app (such as a user authenticating or the app closing). If you want events specific to modules, services or widgets you should consider Services, Directive Controllers, or 3rd Party Libs -
Support
{{}}in all string attributes using$attrs.$observe()
When building a directive that supports string-value-only attributes, always allow the user to provide expressions, likeng-hrefandng-src. -
Extend directives by using Directive Controllers
You can place methods and properties into a directive-controller, and access that same controller from other directives. You can even override methods and properties through this relationship -
Module your code properly
When modules first came out, people started doingapp,app.controllers,app.views,app.services. This is just wrong. It doesn't benefit anything to slice your app across horizontals. If you take out controllers, your app won't work, and the same goes for the others. Instead, group your code into related bundles:app.users,app.projects. Place your related directives, controllers, services etc together, and even add route definitions to those modules. This way if you remove a module, your app will still work, just missing a chunk.