|
| 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