Skip to content

Commit a0250db

Browse files
committed
added directives info
1 parent b7b6cbf commit a0250db

File tree

1 file changed

+102
-7
lines changed

1 file changed

+102
-7
lines changed

README.md

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,108 @@ Many of my styles have been from the many pair programming sessions [Ward Bell](
510510
**[Back to top](#table-of-contents)**
511511
512512
## Directives
513-
TODO
513+
- **Limit 1 Per File**: Create one directive per file. Name the file for the directive.
514+
515+
*Why?*: It is easy to mash all the directives in one file, but difficult to then break those out so some are shared across apps, some across modules, some just for one module. Also easier to maintain.
516+
517+
```javascript
518+
/* avoid */
519+
angular
520+
.module('app.widgets')
521+
522+
/* order directive that is specific to the order module */
523+
.directive('orderCalendarRange', orderCalendarRange)
524+
525+
/* sales directive that can be used anywhere across the sales app */
526+
.directive('salesCustomerInfo', salesCustomerInfo);
527+
528+
/* spinner directive that can be used anywhere across apps */
529+
.directive('sharedSpinner', sharedSpinner);
530+
531+
/* implementation details */
532+
```
533+
534+
```javascript
535+
/* recommended */
536+
537+
angular
538+
.module('sales.order')
539+
/* order directive that is specific to the order module */
540+
.directive('orderCalendarRange', orderCalendarRange)
541+
542+
angular
543+
.module('sales.widgets')
544+
/* sales directive that can be used anywhere across the sales app */
545+
.directive('salesCustomerInfo', salesCustomerInfo);
546+
547+
angular
548+
.module('shared.widgets')
549+
/* spinner directive that can be used anywhere across apps */
550+
.directive('sharedSpinner', sharedSpinner);
551+
552+
/* implementation details */
553+
```
554+
555+
- **Limit DOM Manipulation**: When manipulating the DOM directly, use a directive. If alternative ways can be used such as using CSS to set styles or the [animation services](https://docs.angularjs.org/api/ngAnimate), Angular templating, [`ngShow`](https://docs.angularjs.org/api/ng/directive/ngShow) or [`ngHide`](https://docs.angularjs.org/api/ng/directive/ngHide), then use those instead.
556+
557+
*Why?*: DOM manipulation can be difficult to test, debug, and there are often better ways (e.g. CSS, animations, templating)
558+
559+
- **Restrict to Elements and Attributes**: When creating a directive that makes sense as a standalone element, allow restrict `E` (custom element) and optionally restrict `A` (custom attribute). Generally, if it could be its own control, `E` is appropriate. General guideline is allow `EA` but lean towards implementing as an element when its standalone and as an attribute when it enhances its existing DOM element.
560+
561+
*Why?*: It makes sense.
562+
563+
*Why?*: While we can allow the directive to be used as a class, if the directive is truly acting as an element it makes more sense as an element or at least as an attribute.
564+
565+
```html
566+
<!-- avoid -->
567+
<div class="my-calendar-range"></div>
568+
```
569+
570+
```javascript
571+
/* avoid */
572+
angular
573+
.module('app.widgets')
574+
.directive('myCalendarRange', myCalendarRange);
575+
576+
function myCalendarRange () {
577+
var directive = {
578+
link: link,
579+
templateUrl: '/template/is/located/here.html',
580+
restrict: 'C'
581+
};
582+
return directive;
583+
584+
function link(scope, element, attrs) {
585+
/* */
586+
}
587+
}
588+
```
589+
590+
```html
591+
<!-- recommended -->
592+
<my-calendar-range></my-calendar-range>
593+
<div my-calendar-range></div>
594+
```
595+
596+
```javascript
597+
/* recommended */
598+
angular
599+
.module('app.widgets')
600+
.directive('myCalendarRange', myCalendarRange);
601+
602+
function myCalendarRange () {
603+
var directive = {
604+
link: link,
605+
templateUrl: '/template/is/located/here.html',
606+
restrict: 'EA'
607+
};
608+
return directive;
609+
610+
function link(scope, element, attrs) {
611+
/* */
612+
}
613+
}
614+
```
514615
515616
**[Back to top](#table-of-contents)**
516617
@@ -609,12 +710,6 @@ TODO
609710

610711
```
611712
612-
**[Back to top](#table-of-contents)**
613-
614-
- **Resolve for All Routes**:
615-
616-
TODO
617-
618713
**[Back to top](#table-of-contents)**
619714
620715
## Manual Dependency Injection

0 commit comments

Comments
 (0)