我试图从html中访问属性 - 选项卡标题到我的指令中,并根据该指令更改html模板。 下面是代码 HTML根据属性中的数据更改指令模板
.directive('contentItem', function($compile) {
var locationTemplate = '<div class="container"><div class="row"><div class="col-md-6 form-horizontal" style="border:1px solid"><select id="location" ng-change="detailCtlr.getLocation()" class="form-control" ng-model="detailCtlr.locSelected"><option ng-repeat="option in detailCtlr.typeArr" value="{{option.typeId}}">{{option.type}}</option></select></div></div></div>';
var peopleTemplate = '<div class="container">klfdjsfsjldjs</div>';
var getTemplate = function(contentType) {
switch (contentType) {
case 'Locations':
template = locationTemplate;
break;
case 'People':
template = peopleTemplate;
break;
}
return template;
}
var linker = function(scope, element, attrs) {
console.dir(scope.content);
element.html(getTemplate(scope.content)).show();
$compile(element.contents())(scope);
}
return {
// template: function(tElement, tAttrs) {
// console.log("in template");
// console.log(tAttrs.content);
// console.dir(tAttrs.content);
// console.log("making templte");
//
//
// return getTemplate(tAttrs.content);
// },
restrict: "E",
replace: true,
link: linker,
scope: {
content: '='
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<uib-tab ng-repeat="tab in tabs" select="Ctlr.getTabInfo(tab.title,$index)" active="" heading="{{tab.title}}">
<content-item content="tab.title"></content-item>
</uib-tab>
我能然而访问内容 “tab.title” 在连接功能,使用链接代码angularJS {{detailCtlr.getLocation() }}代码does not work.If我直接返回在模板中的HTML代码的作品,但没有办法得到“tab.title”值...我尝试了一堆像改变范围,传递一个$索引确定选项卡,但似乎没有任何工作..任何建议?提前致谢!
这是一个典型的例子,你应该解释你想要达到的目标,而不是要求特定的解决方案。通常,当您尝试在指令中的不同模板之间切换时,您正在做错某些事情。一个指令应该有1个模板 - 而是根据逻辑包含子指令。 – Etse