Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

javascript - angular dynamic templating directives

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);
    }

};

}]);

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

usages

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

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

*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));
                    });
                }
            }
        };
    }
]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...