2

所以我想制定一个指令来自动包含标签和内容,但我无法获取存储在partials/tabs/tab[x].html中的内容。动态包含模板角度指令

AvailableTabs - 定义为数组常数:

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

我的指令检测被包括在正确的选项卡,并试图以包括选项卡的内容,以及:

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

的问题是该标签的内容是空的,我没有错误。

我的HTML如下:

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

我试图注入的指令$templateCache,但在检索内容时返回undefined,我也试图相对脚本路径的路径走,但仍然undefined

回答

1

因为您错过了'ng-include,用于传递模板名称为string。因为ng-include指令要求输入string

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

将呈现如下

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

太谢谢你了!像魅力一样工作,没有错误,我不知道在哪里看。我必须等5分钟来标记你的问题:) –

+0

@KA_lin发生。无论如何高兴地帮助你..谢谢:) –