Skip to content

Commit 1a31dda

Browse files
committed
Add directive-controller DI example.
1 parent d578dde commit 1a31dda

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ with.
1010

1111
## My JavaScript Demos - I Love JavaScript!
1212

13+
* [Directive Controllers Can Use Dependency Injection In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/directive-controller-di-angularjs/)
1314
* [Implementing Track-By Using A Directive In AngularJS 1.0.8](http://bennadel.github.io/JavaScript-Demos/demos/track-by-directive-ng-repeat-angularjs/)
1415
* [Always Trigger The $destroy Event Before Removing Elements In AngularJS Directives](http://bennadel.github.io/JavaScript-Demos/demos/trigger-destroy-before-removing-element-angularjs/)
1516
* [A Quick Look At How DOM Structure Affects Text Interpolation Watchers In AngularJS](http://bennadel.github.io/JavaScript-Demos/demos/dom-structure-affects-watchers-angularjs/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<!doctype html>
2+
<html ng-app="Demo">
3+
<head>
4+
<meta charset="utf-8" />
5+
6+
<title>
7+
Directive Controllers Can Use Dependency Injection In AngularJS
8+
</title>
9+
</head>
10+
<body>
11+
12+
<h1>
13+
Directive Controllers Can Use Dependency Injection In AngularJS
14+
</h1>
15+
16+
<div bn-test-directive>
17+
Testing this directive.
18+
</div>
19+
20+
21+
<!-- Load scripts. -->
22+
<script type="text/javascript" src="../../vendor/jquery/jquery-2.1.0.min.js"></script>
23+
<script type="text/javascript" src="../../vendor/angularjs/angular-1.2.22.min.js"></script>
24+
<script type="text/javascript">
25+
26+
// Create an application module for our demo.
27+
var app = angular.module( "Demo", [] );
28+
29+
30+
// -------------------------------------------------- //
31+
// -------------------------------------------------- //
32+
33+
34+
// I define a Controller in the dependency injection container. I can be bound
35+
// to an HTML element node using either ngController of as a directive controller.
36+
app.controller(
37+
"my.nameSpace.TestController",
38+
function( $scope, $timeout, $q ) {
39+
40+
console.info( "Controller instantiated" );
41+
42+
// Use $timeout to show that dependency injection worked.
43+
var timer = $timeout(
44+
function handleTimeout() {
45+
46+
return( "Timer executed" );
47+
48+
},
49+
500
50+
);
51+
52+
// Use $q to show that dependency injection worked.
53+
$q.all( [ timer ] ).then(
54+
function handleTimerResolve( resolvedValues ) {
55+
56+
console.log( "Resolution:", resolvedValues[ 0 ] );
57+
58+
}
59+
);
60+
61+
}
62+
);
63+
64+
65+
// -------------------------------------------------- //
66+
// -------------------------------------------------- //
67+
68+
69+
// I bind some JavaScript UI behavior to a given scope. And, in this case, I am
70+
// also binding a Controller to a given UI as well.
71+
app.directive(
72+
"bnTestDirective",
73+
function() {
74+
75+
// I bind the UI behaviors to the local scope.
76+
function link( scope, element, attributes, controller ) {
77+
78+
console.info( "Directive linked" );
79+
80+
}
81+
82+
83+
// Return the directive configuration.
84+
// --
85+
// NOTE: We are providing the Controller name as a value to be pulled out
86+
// of the dependency injection container.
87+
return({
88+
controller: "my.nameSpace.TestController",
89+
link: link,
90+
require: "bnTestDirective",
91+
restrict: "A"
92+
});
93+
94+
}
95+
);
96+
97+
</script>
98+
99+
</body>
100+
</html>
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
122+

0 commit comments

Comments
 (0)