1

Is there a way to add static HTML inside generated template?

I have that code:

<test></test>
<test></test>

<script>
var app = angular.module("myApp", []);
app.directive("test", function() {
    return {
        template:
        '<div class="wrapper">' +
        '<div class="content">' +
        '</div>' +
        '</div>'
    };
});
</script>

... And then, I want to add <p>foo</p> (see yellow note below) inside first <content>, but not inside second. So, the output should be:

<test>
    <div class="wrapper">
        <div class="content">
            foo
        </div>
    </div>
</test>

<test>
    <div class="wrapper">
        <div class="content">
        </div>
    </div>
</test>

How I can do it?

jsFiddle as aksed in the comments.

Note: This is just SO example. In the real code, it should be added more complex HTML markup, not simple <p>foo</p>. For example, it may be added something like: <div class="..."><p>...</p><p>...</p></div>. Or, maybe, some HTML table. That is what I want.

6
  • You want to add how? Programmatically, at what moment? Commented Mar 8, 2016 at 11:23
  • You could for example put a flag or something on the place the text belongs. Then get the template and replace the flag Commented Mar 8, 2016 at 11:25
  • Can you make fiddle for it? Commented Mar 8, 2016 at 11:26
  • @HassanTariq Fiddle added. Also, please see yellow note at the bottom. Thanks. Commented Mar 8, 2016 at 11:47
  • @WouterdenOuden Very interesting, thanks. Commented Mar 8, 2016 at 11:47

3 Answers 3

5

You can use ngTransclude here.

You would define your directive like this:

var app = angular.module("myApp", []);
app.directive("test", function() {
    return {
        transclude: true,
        template:
        `<div class="wrapper">
            <div class="content">
                <ng-transclude></ng-transclude>
            </div>
        </div>`
    };
});

Then in your HTML, you would have:

<test>
    <p>foo</p>
</test>
<test></test>

And the <p>foo</p> will be inserted automatically inside the ng-transclude element in your template.

More information on AngularJS documentation.

Sign up to request clarification or add additional context in comments.

Comments

1

This will solve your problem.

Directive

myApp.directive("test", function($compile) {
    return {
        restrict:'E',
        link: function(scope, element, attrs) {
            var childContent = '';
            if(attrs.content == 'foo')
                childContent = '<p>foo</p>';

            var htmlText = '<div class="wrapper">' +
                '<div class="content">' +
                childContent+
                '</div>' +
                '</div>';

            element.append($compile(htmlText)(scope));
        }
    };
});

Template

<test content='foo'></test>
<test></test>

Comments

0

This is how I would do it. Assuming you already know what content to add for each directive.

<test content="foo"></test>
<test content=""></test>

<script>
var app = angular.module("myApp", []);
app.directive("test", function() {
    return {
       template: function(el, attrs){
        '<div class="wrapper">' +
        '<div class="content">' + '<p>'+attrs.content+'</p>'
        '</div>' +
        '</div>'
       }
    };
});
</script>

That would give you the desired output.

1 Comment

In your code the <p> element is added even if there is no content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.