在指令中动态添加模板

时间:2016-02-10 09:47:22

标签: javascript angularjs

我想在一个angular指令中动态添加一个模板。我已经使用link函数将变量编译成html元素,如answer中所述。

但我无法使其发挥作用。

这是我的HTML:

<spinners></spinners>

这是我的指示:

app.directive('spinners', function() {
    return {
        restrict: 'E',
        link: function(scope, element, attrs) {
            var spinkit = ["<rotating-plane-spinner></rotating-plane-spinner>", "<double-bounce-spinner></double-bounce-spinner>", "<wave-spinner></wave-spinner>", "<wandering-cubes-spinner></wandering-cubes-spinner>", "<pulse-spinner></pulse-spinner>", "<chasing-dots-spinner></chasing-dots-spinner>", "<circle-spinner></circle-spinner>", "<three-bounce-spinner></three-bounce-spinner>", "<cube-grid-spinner></cube-grid-spinner>", "<word-press-spinner></word-press-spinner>", "<fading-circle-spinner></fading-circle-spinner>"];
            var spinner = spinkit[Math.floor(Math.random() * spinkit.length)];
            var el = angular.element(spinner);
            compile(el.contents())(scope);
            element.replaceWith(el);
        }
    };
});

实际上我正在使用Angular-SpinKit在我的加载页面中随机显示微调器。当我直接使用spinkit的单个指令时,它可以工作。但是当我使用上面的方法时,我在html页面中找不到任何内容。

2 个答案:

答案 0 :(得分:0)

问题出现在这一行compile(el.contents())(scope);

compile应为$compile且它是一项服务,应将其注入指令。

所以:$compile(el.contents())(scope);app.directive('spinners', function($compile) {

app.directive('spinners', function($compile) {
    return {
        restrict: 'E',
        link: function(scope, element, attrs) {
            var spinkit = ["<rotating-plane-spinner></rotating-plane-spinner>", "<double-bounce-spinner></double-bounce-spinner>", "<wave-spinner></wave-spinner>", "<wandering-cubes-spinner></wandering-cubes-spinner>", "<pulse-spinner></pulse-spinner>", "<chasing-dots-spinner></chasing-dots-spinner>", "<circle-spinner></circle-spinner>", "<three-bounce-spinner></three-bounce-spinner>", "<cube-grid-spinner></cube-grid-spinner>", "<word-press-spinner></word-press-spinner>", "<fading-circle-spinner></fading-circle-spinner>"];
            var spinner = spinkit[Math.floor(Math.random() * spinkit.length)];
            var el = angular.element(spinner);
            $compile(el.contents())(scope);
            element.replaceWith(el);
        }
    };
});

答案 1 :(得分:0)

试试这个:

app.directive('spinners', function($compile) {
return {
    restrict: 'E',
    link: function(scope, element, attrs) {
        var spinkit = ["<rotating-plane-spinner></rotating-plane-spinner>", "<double-bounce-spinner></double-bounce-spinner>", "<wave-spinner></wave-spinner>", "<wandering-cubes-spinner></wandering-cubes-spinner>", "<pulse-spinner></pulse-spinner>", "<chasing-dots-spinner></chasing-dots-spinner>", "<circle-spinner></circle-spinner>", "<three-bounce-spinner></three-bounce-spinner>", "<cube-grid-spinner></cube-grid-spinner>", "<word-press-spinner></word-press-spinner>", "<fading-circle-spinner></fading-circle-spinner>"];
        var spinner = spinkit[Math.floor(Math.random() * spinkit.length)];
        var el = angular.element(spinner);
        element.replaceWith(el);
        $compile(el)(scope);

    }
};
});