且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有角度的:如何在< pre>中嵌套模板标签?

更新时间:2023-10-25 22:14:04

您可以使用两个指令:一个是一种ngInclude,另一个则等待第一个加载内容并将其自身替换为pre ( http://plnkr.co/edit/fPy4M0ZK94erF31EeObE ):

You can do with a two directives: one is a sort of ngInclude, the other waits for the first to load the content and replaces itself with a pre (http://plnkr.co/edit/fPy4M0ZK94erF31EeObE):

module.directive('myPre', function() {
  return {
    controller: function() {},
    restrict: 'E',
    link: function(scope, element, attrs, controller) {
      controller.checkContent = function() {
        if (!element.children().length) {
          element.replaceWith('<pre>' + element.text() + '</pre>');
        }
      }
    }
  }
})
.directive('myPreTemplate', function($http) {
  return {
    require: '^myPre',
    restrict: 'E',
    link: function(scope, element, attrs, myPre) {
      $http.get(attrs.src).then(function(res) {
        element.replaceWith(res.data);
        myPre.checkContent();
      });
    }
  }
});

您可以在此处使用它:

<my-pre>
  Code here...
  <my-pre-template src="common.html"></my-pre-template>
  ...and here...
  <my-pre-template src="common.html"></my-pre-template>
  ...and here again
</my-pre>

编辑:呈现通用模板的内容,***的方法是使用小胡子(插件已更新):

EDIT: to render the content of common template, the best way is using mustache (plunker updated):

...
$http.get(attrs.src).then(function(res) {
  var rendered = Mustache.render(res, scope);
  element.replaceWith(rendered);
  myPre.checkContent();
});
...