4
votes

Where does Angular store directive templates?

I have a module/directive and I want to be able to get the template it uses in another directive. Specifically I want to view the raw HTML and the names of the scoped variables

Some Directive

var someModule = angular.module('someModule', []);

someModule.controller('someModuleCtrl', [ '$scope',
    function ($scope) {
        'use strict';
        //Do stuff
        };
    }]);

someModule.directive('someModule', [
    function () {
        'use strict';
        return {
            restrict: 'AE',
            template: ' some HTML and {{AngularVar}}',
            scope: {
                input1: '=',
                input2: '=',
                optional1: '=?'
            },
            controller: "someModuleCtrl"
        };
    }]);

Some other directive where I want to see the raw HTML of the other directive's tempalte

var myModule = angular.module('MyModule', []);
myModule.controller('MyModuleCtrl', ['$scope', 
    function ($scope) {
        console.log(--------someModule.directives['someModule'].template---------);
    }]);
1
I don't think this is possible.Chandermani
I know it has to be possible... Angular must store it somewhere...Cheruvian

1 Answers

0
votes

Directives are injectable as nameDirective, here's an example:

.directive("one", function () {
  return {
    template: "<div>{{something}}</div>",
    scope: {
      a: "@",
      b: "=",
      c: "&test"
    }
  };
})
.directive("two", function (oneDirective) {
  return function link () {
    var d = oneDirective[0];
    console.log(d.scope, d.template)
  };
});

/*
[object Object] {
  a: "@",
  b: "=",
  c: "&test"
}
"<div>{{something}}</div>"
*/

JSBin.