AngularJS指令包含文件内容



我正试图在AngularJS中为一个小的模板问题创建一个指令。在我的HTML文件中,我有这样的:

<div myinclude="header.tpl.html"></div>

我的指令应该把这个转换成那个:

<div>{content of file: header.tpl.html}</div>

这就是我已经走了多远,但我想,我错过了一块:

    myApp.directive('myinclude', function () {
    return {
        compile: function compile( tElement, tAttributes ) {
            console.log(tAttributes.myinclude); // logs filename
            return {
                templateURL: tAttributes.myinclude
            };
         }
     };
});

以前有人做过这样的事情,愿意分享吗?

这是正在工作的plunker

试试这样的东西:

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
    $scope.setValue = function(ele) {
     console.log(ele)
    };
});
app.directive("myInclude",function(){
 return {
     restrict : 'A',
      link : function(scope, element, attrs){
          scope.getContentUrl = attrs["myInclude"];
      },
      template: "content of file : {{getContentUrl}}"
    }
});

HTML

<!DOCTYPE html>
<html ng-app="plunker">
  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
    <script src="app.js"></script>
  </head>
  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
 <div my-include="header.tpl.html"></div>
  </body>
</html>

它正在工作。。。

app.directive('productDescription',function(){
return {
restrict:'E',
templateUrl: 'product-description.html'
};
});