我正在为一个知识库应用程序开发一组驱动工具栏的角度指令。
我遇到的问题是如何让我的父指令处理嵌套在其子指令中的关于父指令模板的子指令。
我想要有这样一个概念,
-Toolbar ->Button Group ->->Button
所以我有三条指令
工具栏xyz-工具栏-按钮-组xyz-工具栏-按钮
工具栏指令是限制A,仅属性。按钮组和按钮限制E(仅限元素)。
每个指令都有独立的作用域(我通过指令中的控制器链接传递东西)。
问题是,我想使用一个模板在由按钮组指令(内联),并让它包括任何按钮。
例如,我有这样的标记(这是asp.net MVC中的主模板),它是一个部分视图,加载在工具栏将呈现的标题中。
<kb-toolbar>
<kb-toolbar-button-group>
<kb-toolbar-button kb-toggle="ng-class: Button.Toggled ? 'fa fa-2x fa-save': 'fa fa-2x fa-edit'" ng-attr-title="Button.Title">{{!Button.IsToggle ? Button.Text: ''}}</kb-toolbar-button>
</kb-toolbar-button-group>
</kb-toolbar>现在,我有一个kb-工具栏指令。
app.modules.main.directive("kbToolbar", function () {
return {
scope: {},
restrict: 'A',
link: function ($scope, element, attrs) {
},
controller: function ($scope) {
var buttonGroups = new Array();
this.addButtonGroup = function (buttonGroup) {
buttonGroups.push(buttonGroup);
}
$scope.buttonGroups = buttonGroups;
}
}
});然后是按钮组
app.modules.main.directive("kbToolbarButtonGroup", function () {
return {
scope: {},
restrict: 'E',
replace: true,
link: function ($scope, element, attrs) {
console.log(element);
},
compile: function(element, attrs) {
var content = element.children();
console.log(content);
},
controller: function ($scope) {
//TODO
},
template: '<ul class="nav navbar-nav">' +
+ '' + //I Don't know what to put in here, this is where the child directive needs to go
'</ul>'
};
});最后是按钮
app.modules.main.directive("kbToolbarButton", function () {
return {
scope: {},
restrict: 'E',
replace: true,
link: function ($scope, element, attrs) {
},
controller: function ($scope) {
//TODO
},
template: '<li><a href="">SomeButtonCompiled</a></li>'
}
});因此,基本问题是kb-工具栏按钮组呈现无序列表,但不包含子列表。因此,我需要在该指令上的模板"“中添加一些内容,以使其包含其中的kb-工具栏-按钮。
发布于 2015-06-23 18:45:10
您可以使用transclude so kbToolbarButtonGroup指令来实现这一点,以便在您提到ng-transclude的元素中呈现子内容。
指令
app.modules.main.directive("kbToolbarButtonGroup", function () {
return {
scope: {},
restrict: 'E',
replace: true,
transclude: true,
link: function ($scope, element, attrs) {
console.log(element);
},
compile: function(element, attrs) {
var content = element.children();
console.log(content);
},
controller: function ($scope) {
//TODO
},
template: '<ul class="nav navbar-nav">' +
+ '<ng-transclude></ng-transclude>' //added transclude here that will load child template here
+'</ul>'
};
});https://stackoverflow.com/questions/31010762
复制相似问题