Dynamic include template angular directive

ka_lin

So I want to make a directive to automatically include tabs and content, but I am unable to get the content stored in partials/tabs/tab[x].html.

AvailableTabs - constant defined as an array :

myApp.constant("availableTabs", [
    {name:'tab1', title:'One', content:'partials/tabs/tab1.html'},
    {name:'tab2', title:'Two', content:'partials/tabs/tab2.html'},
    {name:'tab3', title:'Three', content:'partials/tabs/tab3.html'}
]);

My directive detects the correct tab to be included, and tries to include the tab's content as well:

 myApp.directive('myTabs',['availableTabs','$templateCache', function(availableTabs, $templateCache) {
        return {
            restrict: 'A',
            template: function (elem, attr) {
                for (index = 0; index < availableTabs.length; ++index) {
                    if(availableTabs[index].name == attr.myTabs) {
                        return '<tab heading="'+availableTabs[index].title+'" ng-show="1"> ' +
                                '<div ng-include="'+availableTabs[index].content+'"></div>'+
                            '</tab>';
                       //return '<tab heading="'+availableTabs[index].title+'" ng-show="1"> ' +
                       //    $templateCache.get(availableTabs[index].content)+
                       //    '</tab>';
                    }
                }
            },
    };
}]);

The problem is that the content of the tab is empty and I have no errors.

My html is the following:

<tabset>
  <div my-tabs="tab1"></div>
  <div my-tabs="tab2"></div>
  <div my-tabs="tab3"></div>
</tabset>

I tried injecting $templateCache in the directive but it returns undefined when retrieving the content, I also tried to take the path relative to the script path but still undefined.

Pankaj Parkar

Because you had missed ' in ng-include, for passing template name as string. Because ng-include directive requires string as input.

ng-include="\''+availableTabs[index].content+'\'"

will be render as below

ng-inlcude="'partials/tabs/tab1.html'"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamic template in AngularJS Directive (Not templatUrl)

From Dev

Angular js directive dynamic template binding

From Dev

Angular Directive - Dynamic Controller

From Dev

Play framework dynamic template include

From Dev

Load Angular Directive Template Async

From Dev

angular - update directive template on click

From Dev

Dynamic item template for paginator directive

From Dev

Using a angular directive on the template of another directive?

From Dev

angular directive template not loading

From Dev

Angular append directive template to table

From Dev

AngularJs Directive with dynamic Controller and template

From Dev

Dynamic path for data-template in angular-strap bs-popover directive

From Dev

Angular - Directive with dynamic template

From Dev

Angular Directive Template and Child Directive inclusion

From Dev

Bind angular directive template to a event

From Dev

Angular directive template included with ng-include

From Dev

Angular Directive with ng-include template

From Dev

Disabling the checkbox in angular directive template

From Dev

Including a directive in Angular - Missing template?

From Dev

Angular : Directive code not rendering the template

From Dev

How do I include other directives in the template of my customer directive in Angular?

From Dev

Play framework dynamic template include

From Dev

Using a angular directive on the template of another directive?

From Dev

Angular Directive parent template?

From Dev

Dynamic path for data-template in angular-strap bs-popover directive

From Dev

How to include template within a directive template?

From Dev

Angular directive template included with ng-include

From Dev

Dynamic ui-sref in directive template Angular Js

From Dev

Dynamic field in directive template (angularjs)

Related Related

  1. 1

    Dynamic template in AngularJS Directive (Not templatUrl)

  2. 2

    Angular js directive dynamic template binding

  3. 3

    Angular Directive - Dynamic Controller

  4. 4

    Play framework dynamic template include

  5. 5

    Load Angular Directive Template Async

  6. 6

    angular - update directive template on click

  7. 7

    Dynamic item template for paginator directive

  8. 8

    Using a angular directive on the template of another directive?

  9. 9

    angular directive template not loading

  10. 10

    Angular append directive template to table

  11. 11

    AngularJs Directive with dynamic Controller and template

  12. 12

    Dynamic path for data-template in angular-strap bs-popover directive

  13. 13

    Angular - Directive with dynamic template

  14. 14

    Angular Directive Template and Child Directive inclusion

  15. 15

    Bind angular directive template to a event

  16. 16

    Angular directive template included with ng-include

  17. 17

    Angular Directive with ng-include template

  18. 18

    Disabling the checkbox in angular directive template

  19. 19

    Including a directive in Angular - Missing template?

  20. 20

    Angular : Directive code not rendering the template

  21. 21

    How do I include other directives in the template of my customer directive in Angular?

  22. 22

    Play framework dynamic template include

  23. 23

    Using a angular directive on the template of another directive?

  24. 24

    Angular Directive parent template?

  25. 25

    Dynamic path for data-template in angular-strap bs-popover directive

  26. 26

    How to include template within a directive template?

  27. 27

    Angular directive template included with ng-include

  28. 28

    Dynamic ui-sref in directive template Angular Js

  29. 29

    Dynamic field in directive template (angularjs)

HotTag

Archive