How to create a directive with a dynamic template in AngularJS?

后端 未结 7 847
傲寒
傲寒 2020-12-02 14:45

How can I create a directive with a dynamic template?

\'use strict\';

app.directive(\'ngFormField\', function($compil         


        
相关标签:
7条回答
  • 2020-12-02 15:04

    One way is using a template function in your directive:

    ...
    template: function(tElem, tAttrs){
        return '<div ng-include="' + tAttrs.template + '" />';
    }
    ...
    
    0 讨论(0)
  • 2020-12-02 15:07

    I have been in the same situation, my complete solution has been posted here

    Basically I load a template in the directive in this way

    var tpl = '' + 
        <div ng-if="maxLength" 
            ng-include="\'length.tpl.html\'">
        </div>' +
        '<div ng-if="required" 
            ng-include="\'required.tpl.html\'">
        </div>';
    

    then according to the value of maxLength and required I can dynamically load one of the 2 templates, only one of them at a time is shown if necessary.

    I heope it helps.

    0 讨论(0)
  • 2020-12-02 15:10

    Had a similar need. $compile does the job. (Not completely sure if this is "THE" way to do it, still working my way through angular)

    http://jsbin.com/ebuhuv/7/edit - my exploration test.

    One thing to note (per my example), one of my requirements was that the template would change based on a type attribute once you clicked save, and the templates were very different. So though, you get the data binding, if need a new template in there, you will have to recompile.

    0 讨论(0)
  • 2020-12-02 15:13

    You should move your switch into the template by using the 'ng-switch' directive:

    module.directive('testForm', function() {
        return {
            restrict: 'E',
            controllerAs: 'form',
            controller: function ($scope) {
                console.log("Form controller initialization");
                var self = this;
                this.fields = {};
                this.addField = function(field) {
                    console.log("New field: ", field);
                    self.fields[field.name] = field;
                };
            }
        }
    });
    
    module.directive('formField', function () {
        return {
            require: "^testForm",
            template:
                '<div ng-switch="field.fieldType">' +
                '    <span>{{title}}:</span>' +
                '    <input' +
                '        ng-switch-when="text"' +
                '        name="{{field.name}}"' +
                '        type="text"' +
                '        ng-model="field.value"' +
                '    />' +
                '    <select' +
                '        ng-switch-when="select"' +
                '        name="{{field.name}}"' +
                '        ng-model="field.value"' +
                '        ng-options="option for option in options">' +
                '        <option value=""></option>' +
                '    </select>' +
                '</div>',
            restrict: 'E',
            replace: true,
            scope: {
                fieldType: "@",
                title: "@",
                name: "@",
                value: "@",
                options: "=",
            },
            link: function($scope, $element, $attrs, form) {
                $scope.field = $scope;
                form.addField($scope);
            }
        };
    });
    

    It can be use like this:

    <test-form>
        <div>
            User '{{!form.fields.email.value}}' will be a {{!form.fields.role.value}}
        </div>
        <form-field title="Email" name="email" field-type="text" value="me@example.com"></form-field>
        <form-field title="Role" name="role" field-type="select" options="['Cook', 'Eater']"></form-field>
        <form-field title="Sex" name="sex" field-type="select" options="['Awesome', 'So-so', 'awful']"></form-field>
    </test-form>
    
    0 讨论(0)
  • 2020-12-02 15:14

    If you want to use AngularJs Directive with dynamic template, you can use those answers,But here is more professional and legal syntax of it.You can use templateUrl not only with single value.You can use it as a function,which returns a value as url.That function has some arguments,which you can use.

    http://www.w3docs.com/snippets/angularjs/dynamically-change-template-url-in-angularjs-directives.html

    0 讨论(0)
  • 2020-12-02 15:19

    i've used the $templateCache to accomplish something similar. i put several ng-templates in a single html file, which i reference using the directive's templateUrl. that ensures the html is available to the template cache. then i can simply select by id to get the ng-template i want.

    template.html:

    <script type="text/ng-template" id=“foo”>
    foo
    </script>
    
    <script type="text/ng-template" id=“bar”>
    bar
    </script>
    

    directive:

    myapp.directive(‘foobardirective’, ['$compile', '$templateCache', function ($compile, $templateCache) {
    
        var getTemplate = function(data) {
            // use data to determine which template to use
            var templateid = 'foo';
            var template = $templateCache.get(templateid);
            return template;
        }
    
        return {
            templateUrl: 'views/partials/template.html',
            scope: {data: '='},
            restrict: 'E',
            link: function(scope, element) {
                var template = getTemplate(scope.data);
    
                element.html(template);
                $compile(element.contents())(scope);
            }
        };
    }]);
    
    0 讨论(0)
提交回复
热议问题