4

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---------);
    }]);
4

1 回答 1

0

指令是可注入的nameDirective,这里有一个例子:

.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>"
*/

杰宾

于 2014-09-20T02:35:53.097 回答