Customizing the template within a Directive

前端 未结 4 1543
醉话见心
醉话见心 2020-11-28 00:25

I have a form that is using markup from Bootstrap, like the following:

Legend tex
4条回答
  •  半阙折子戏
    2020-11-28 01:09

    Tried to use the solution proposed by Misko, but in my situation, some attributes, which needed to be merged into my template html, were themselves directives.

    Unfortunately, not all of the directives referenced by the resulting template did work correctly. I did not have enough time to dive into angular code and find out the root cause, but found a workaround, which could potentially be helpful.

    The solution was to move the code, which creates the template html, from compile to a template function. Example based on code from above:

        angular.module('formComponents', [])
      .directive('formInput', function() {
        return {
            restrict: 'E',
            template: function(element, attrs) {
               var type = attrs.type || 'text';
                var required = attrs.hasOwnProperty('required') ? "required='required'" : "";
                var htmlText = '
    ' + '' + '
    ' + '' + '
    ' + '
    '; return htmlText; } compile: function(element, attrs) { //do whatever else is necessary } } })

提交回复
热议问题