showcase: compile/prelink/postlink/controller calling order
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 13 Sep 2015 18:24:11 +0000 (20:24 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 13 Sep 2015 18:24:11 +0000 (20:24 +0200)
When having parent and child directives

angularjs/showcase/src/showcase/app/welcome/welcome.html
angularjs/showcase/src/showcase/app/widgets/example-child.directive.html [new file with mode: 0644]
angularjs/showcase/src/showcase/app/widgets/example-child.directive.js
angularjs/showcase/src/showcase/app/widgets/example-parent.directive.html [new file with mode: 0644]
angularjs/showcase/src/showcase/app/widgets/example-parent.directive.js [new file with mode: 0644]

index f47e20f..b2e2f94 100644 (file)
@@ -8,4 +8,34 @@
     <div class="col-md-10">
     </div>
   </div>
-</div>
\ No newline at end of file
+
+  <code>
+    // BE CAREFUL, THE LOADING ORDER IS DIFFERENT WHEN USING templateUrl TO WHEN USING template!!! <br><br>
+
+    // ****** console output when using templateUrl in parent and child directives: ****** <br>
+    //XHR finished loading: GET "http://localhost:9000/app/widgets/example-parent.directive.html" <br>
+    //example-parent.directive.js:67 PARENT-DIRECTIVE: compile <br>
+    //example-parent.directive.js:91 PARENT-DIRECTIVE: controller <br>
+    //example-parent.directive.js:70 PARENT-DIRECTIVE: preLink <br>
+    //example-parent.directive.js:73 PARENT-DIRECTIVE: postLink <br>
+    //XHR finished loading: GET "http://localhost:9000/app/widgets/example-child.directive.html". <br>
+    //example-child.directive.js:63 CHILD-DIRECTIVE: compile <br>
+    //example-child.directive.js:87 CHILD-DIRECTIVE: controller <br>
+    //example-child.directive.js:66 CHILD-DIRECTIVE: preLink <br>
+    //example-child.directive.js:69 CHILD-DIRECTIVE: postLink <br><br>
+
+    // ****** console output when using template in parent and child directives: ****** <br>
+    //example-parent.directive.js:67 PARENT-DIRECTIVE: compile <br>
+    //example-child.directive.js:63 CHILD-DIRECTIVE: compile <br>
+    //example-parent.directive.js:91 PARENT-DIRECTIVE: controller <br>
+    //example-parent.directive.js:70 PARENT-DIRECTIVE: preLink <br>
+    //example-child.directive.js:87 CHILD-DIRECTIVE: controller <br>
+    //example-child.directive.js:66 CHILD-DIRECTIVE: preLink <br>
+    //example-child.directive.js:69 CHILD-DIRECTIVE: postLink <br>
+    //example-parent.directive.js:73 PARENT-DIRECTIVE: postLink <br><br>
+  </code>
+  <label>parent directive:</label>
+  <parent-directive>
+  </parent-directive>
+
+</div>
diff --git a/angularjs/showcase/src/showcase/app/widgets/example-child.directive.html b/angularjs/showcase/src/showcase/app/widgets/example-child.directive.html
new file mode 100644 (file)
index 0000000..8ff52eb
--- /dev/null
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<div>child directive html template</div>
index 6d65816..e0be270 100644 (file)
@@ -3,11 +3,11 @@
 
   angular
     .module('app.widgets')
-    .directive('parentDirective', parentDirective);
+    .directive('childDirective', childDirective);
 
   /**
    * @ngdoc directive
-   * @name app.widgets.directive:parentDirective
+   * @name app.widgets.directive:childDirective
    * @restrict EA
    * @requires $scope
    *
    * </p>
    *
    * @description
-   * Controller directive example.
+   * Controller child directive example.
    *
-   * @element parent-directive
+   * @element child-directive
    *
    * @example
     <example name="controller-directive" module="app.widgets">
       <file name="index.html">
-        <parent-directive>
+        <child-directive>
       </file>
     </example>
    */
-  function parentDirective() {
+  function childDirective() {
     return {
       restrict: 'EA',
-      templateUrl: 'app/widgets/example-parent.directive.html',
+      templateUrl: 'app/widgets/example-child.directive.html',
+
+      // BE CAREFUL, THE LOADING ORDER IS DIFFERENT WHEN USING templateUrl TO WHEN USING template!!!
+
+      // ****** console output when using templateUrl in parent and child directives: ******
+      //XHR finished loading: GET "http://localhost:9000/app/widgets/example-parent.directive.html"
+      //example-parent.directive.js:67 PARENT-DIRECTIVE: compile
+      //example-parent.directive.js:91 PARENT-DIRECTIVE: controller
+      //example-parent.directive.js:70 PARENT-DIRECTIVE: preLink
+      //example-parent.directive.js:73 PARENT-DIRECTIVE: postLink
+      //XHR finished loading: GET "http://localhost:9000/app/widgets/example-child.directive.html".
+      //example-child.directive.js:63 CHILD-DIRECTIVE: compile
+      //example-child.directive.js:87 CHILD-DIRECTIVE: controller
+      //example-child.directive.js:66 CHILD-DIRECTIVE: preLink
+      //example-child.directive.js:69 CHILD-DIRECTIVE: postLink
+
+      // ****** console output when using template in parent and child directives: ******
+      //example-parent.directive.js:67 PARENT-DIRECTIVE: compile
+      //example-child.directive.js:63 CHILD-DIRECTIVE: compile
+      //example-parent.directive.js:91 PARENT-DIRECTIVE: controller
+      //example-parent.directive.js:70 PARENT-DIRECTIVE: preLink
+      //example-child.directive.js:87 CHILD-DIRECTIVE: controller
+      //example-child.directive.js:66 CHILD-DIRECTIVE: preLink
+      //example-child.directive.js:69 CHILD-DIRECTIVE: postLink
+      //example-parent.directive.js:73 PARENT-DIRECTIVE: postLink
+
+      //template: '<div>child directive html template</div>',
       link: linkFunc,
       compile: function compile(element, attributes, transcludeFn) {
-        console.log('PARENT-DIRECTIVE: compile');
+        console.log('CHILD-DIRECTIVE: compile');
         return {
           pre: function preLink(scope, element, attributes, controller, transcludeFn) {
-            console.log('PARENT-DIRECTIVE: preLink');
+            console.log('CHILD-DIRECTIVE: preLink');
           },
           post: function postLink(scope, element, attributes, controller, transcludeFn) {
-            console.log('PARENT-DIRECTIVE: postLink');
+            console.log('CHILD-DIRECTIVE: postLink');
           }
         };
       },
-      controller: ExampleParentController,
+      controller: ExampleChildController,
       scope: {
         max: '='
       }
     };
 
     function linkFunc(scope, el, attr, ctrl) {
-      console.log('PARENT-DIRECTIVE: linkFunc');
+      // This function will never be used because we are implementing the compile attribute.
+      console.log('CHILD-DIRECTIVE: linkFunc');
     }
   }
 
   /* @ngInject */
-  function ExampleParentController($scope) {
-    console.log('PARENT-DIRECTIVE: controller');
+  function ExampleChildController($scope) {
+    console.log('CHILD-DIRECTIVE: controller');
 
     $scope.min = 3;
   }
diff --git a/angularjs/showcase/src/showcase/app/widgets/example-parent.directive.html b/angularjs/showcase/src/showcase/app/widgets/example-parent.directive.html
new file mode 100644 (file)
index 0000000..bbc83cd
--- /dev/null
@@ -0,0 +1,5 @@
+<!DOCTYPE html>
+<div>parent directive html template</div>
+<label>child directive:</label>
+<child-directive>
+</child-directive>
diff --git a/angularjs/showcase/src/showcase/app/widgets/example-parent.directive.js b/angularjs/showcase/src/showcase/app/widgets/example-parent.directive.js
new file mode 100644 (file)
index 0000000..e7f79f0
--- /dev/null
@@ -0,0 +1,96 @@
+(function () {
+  'use strict';
+
+  angular
+    .module('app.widgets')
+    .directive('parentDirective', parentDirective);
+
+  /**
+   * @ngdoc directive
+   * @name app.widgets.directive:parentDirective
+   * @restrict EA
+   * @requires $scope
+   *
+   * <p>
+   * <br>
+   * {@link https://docs.angularjs.org/api/ng/type/$rootScope.Scope $scope}
+   * </p>
+   *
+   * @description
+   * Controller parent directive example.
+   *
+   * @element parent-directive
+   *
+   * @example
+    <example name="controller-directive" module="app.widgets">
+      <file name="index.html">
+        <parent-directive>
+      </file>
+    </example>
+   */
+  function parentDirective() {
+    return {
+      restrict: 'EA',
+      templateUrl: 'app/widgets/example-parent.directive.html',
+
+      // BE CAREFUL, THE LOADING ORDER IS DIFFERENT WHEN USING templateUrl TO WHEN USING template!!!
+
+      // ****** console output when using templateUrl in parent and child directives: ******
+      //XHR finished loading: GET "http://localhost:9000/app/widgets/example-parent.directive.html"
+      //example-parent.directive.js:67 PARENT-DIRECTIVE: compile
+      //example-parent.directive.js:91 PARENT-DIRECTIVE: controller
+      //example-parent.directive.js:70 PARENT-DIRECTIVE: preLink
+      //example-parent.directive.js:73 PARENT-DIRECTIVE: postLink
+      //XHR finished loading: GET "http://localhost:9000/app/widgets/example-child.directive.html".
+      //example-child.directive.js:63 CHILD-DIRECTIVE: compile
+      //example-child.directive.js:87 CHILD-DIRECTIVE: controller
+      //example-child.directive.js:66 CHILD-DIRECTIVE: preLink
+      //example-child.directive.js:69 CHILD-DIRECTIVE: postLink
+
+      // ****** console output when using template in parent and child directives: ******
+      //example-parent.directive.js:67 PARENT-DIRECTIVE: compile
+      //example-child.directive.js:63 CHILD-DIRECTIVE: compile
+      //example-parent.directive.js:91 PARENT-DIRECTIVE: controller
+      //example-parent.directive.js:70 PARENT-DIRECTIVE: preLink
+      //example-child.directive.js:87 CHILD-DIRECTIVE: controller
+      //example-child.directive.js:66 CHILD-DIRECTIVE: preLink
+      //example-child.directive.js:69 CHILD-DIRECTIVE: postLink
+      //example-parent.directive.js:73 PARENT-DIRECTIVE: postLink
+
+      //template: '' +
+      //'<div>parent directive html template</div>' +
+      //'<label>child directive:</label>' +
+      //'<child-directive>' +
+      //'</child-directive>',
+      link: linkFunc,
+      compile: function compile(element, attributes, transcludeFn) {
+        console.log('PARENT-DIRECTIVE: compile');
+        return {
+          pre: function preLink(scope, element, attributes, controller, transcludeFn) {
+            console.log('PARENT-DIRECTIVE: preLink');
+          },
+          post: function postLink(scope, element, attributes, controller, transcludeFn) {
+            console.log('PARENT-DIRECTIVE: postLink');
+          }
+        };
+      },
+      controller: ExampleParentController,
+      scope: {
+        max: '='
+      }
+    };
+
+    function linkFunc(scope, el, attr, ctrl) {
+      // This function will never be used because we are implementing the compile attribute.
+      console.log('PARENT-DIRECTIVE: linkFunc');
+    }
+  }
+
+  /* @ngInject */
+  function ExampleParentController($scope) {
+    console.log('PARENT-DIRECTIVE: controller');
+
+    $scope.min = 3;
+  }
+
+})();