角动态模板指令 [英] angular dynamic templating directives

查看:91
本文介绍了角动态模板指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同字段类型的列表,我想根据类型应用模板。如果我使用这样的内联模板,我可以使它工作:

I have a list of different field types and I want to apply a template based on type. I can get it to work if I use inline templates like this:

flowPageModule.directive('myField', ['$compile','$http', '$templateCache', function($compile, $http, $templateCache) {

var inlineTemplateMapping = {
        select: '<div><span> {{label}} &nbsp <select ng-model="metadata.options[0]" ng-options="o.text for o in metadata.options"></select> </span></div>',
        text: '<div><span> {{label}} &nbsp <input type="text" /> </span></div>'
    }

return {

    restrict: 'E',
    replace: true,
    transclude: true,
    scope: { type: '=', label: '=', metadata: '=' },
    link: function (scope, element, attrs) {

        if (scope.metadata) { alert(scope.metadata.options[0].text); }
        var templateLiteral = inlineTemplateMapping[scope.type];
        element.html(templateLiteral);
        $compile(element.contents())(scope);
    }

};

}]);

我会如果我可以使用$ http服务从文件中检索模板,真的很喜欢它。我已经尝试了下面的内容,但我总是得到一个Type错误。

I would really like for it to work if i could use the $http service to retrieve a template from a file. I have tried what is below, but I always get a Type error.

flowPageModule.directive('myField', ['$compile','$http', '$templateCache', function($compile, $http, $templateCache) {

var baseUrl = 'directives/field/',
    typeTemplateMapping = {
        text: 'my-field-text.html',
        select: 'my-field-select.html'
    };

return {

    restrict: 'E',
    replace: true,
    transclude: true,
    scope: { type: '=', label: '=', metadata: '=' },
    link: function (scope, element, attrs) {

            var templateUrl = baseUrl + typeTemplateMapping[scope.type];
            $http.get(templateUrl, {cache: $templateCache}).success( function(html) {
                element.html();
                $compile(element.contents())(scope);
            }); 
    }

};

}]);

为什么会发生这种情况?

Why does this happen?

推荐答案

我得到了创造这个的报酬,但实际上没有人真正关心实施(好还是坏?)..我可以自由地与社区分享这个。
我从另一个SO答案调整了loadT()函数。你可以找到它搜索动态模板角度。此处的调整允许您在$ http承诺未返回时显示空组件。

I got paid to create this, but since actually no one really care about the implementation (good or bad?)...I am feeling free to share this to the community. I adapted the loadT() function from another SO answer. You can find it searching for "dynamic template angular". The adapation here allows you to display an empty component while $http promise has not returned.

用法

<p dynamic template="'template/file.html'"></p>

<p dynamic template="someObjectInController.value"></p>

* transclusion不起作用。所以随意添加/更正。问题在于缺少transclude函数的文档。

*transclusion doesn't work. So feel free to add/correct. The issue lies in the lack of documentation for the transclude function.

cpanel.directive("dynamic",  ["$compile", '$templateCache', '$http',
    function ($compile, $templateCache, $http) {
        return {
            priority: 0,
            restrict: 'EA',
            replace: true,
            transclude: true,
            controller: 'SomeController',
            scope: {
                template: "@template"
            },
            link: function (scope, iElement, iAttrs) {

                scope.$parent.$watch(iAttrs.template, function (newvalue, oldvalue) {
                    if ((oldvalue !== newvalue) && (newvalue !== undefined) && (newvalue !== null)) {
                        loadT(newvalue);
                    } else if ((newvalue !== undefined) && (newvalue !== null)) {
                        loadT(newvalue);
                    } else {
                        //TODO
                    }
                }, true);

                function loadT(it) {
                    $http.get(it, { cache: $templateCache}).success(function (templateContent) {
                        iElement.replaceWith($compile(templateContent)(scope));
                    });
                }
            }
        };
    }
]);

这篇关于角动态模板指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆