2

假设我有一个 XML 文档,看起来像

<something>
  <something-else />
</something>

我想将它直接插入到 HTML 文档中。关于元素的外观有一定的规则<something>(例如,它们div的宽度应该是100px黑色背景,并且应该在单击时展开)。Angular 指令似乎是处理这个问题的好方法 - 只需制作一个something指令。唯一的问题是 XML 是从服务器加载的,所以我不能事先制定指令。我尝试将指令创建插入控制器,但它(预期)不适用于这个简单的示例:

$scope.makeTheDirective = function () {
    module.directive('dir', function () {
        return {
            replace: true,
            template: '<div style="background:black;width:100px;height:100px;"></div>',
            link: function () {
                console.log('making the directive');
            }
        }
    });

    $(document.body).append($compile('<dir>This is a test</dir>'));
}

在文档中适当调用。如何让 Angular 识别加载应用程序后创建的指令,以便我可以将$compiled HTML 添加到 DOM?

4

0 回答 0