首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角指令模板和子指令包含

角指令模板和子指令包含
EN

Stack Overflow用户
提问于 2015-06-23 18:32:29
回答 1查看 3.2K关注 0票数 2

我正在为一个知识库应用程序开发一组驱动工具栏的角度指令。

我遇到的问题是如何让我的父指令处理嵌套在其子指令中的关于父指令模板的子指令。

我想要有这样一个概念,

-Toolbar ->Button Group ->->Button

所以我有三条指令

工具栏xyz-工具栏-按钮-组xyz-工具栏-按钮

工具栏指令是限制A,仅属性。按钮组和按钮限制E(仅限元素)。

每个指令都有独立的作用域(我通过指令中的控制器链接传递东西)。

问题是,我想使用一个模板在由按钮组指令(内联),并让它包括任何按钮。

例如,我有这样的标记(这是asp.net MVC中的主模板),它是一个部分视图,加载在工具栏将呈现的标题中。

代码语言:javascript
复制
<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-工具栏指令。

代码语言:javascript
复制
    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;
        }
    }
});

然后是按钮组

代码语言:javascript
复制
    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>'
    };
});

最后是按钮

代码语言:javascript
复制
    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-工具栏-按钮。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-23 18:45:10

您可以使用transclude so kbToolbarButtonGroup指令来实现这一点,以便在您提到ng-transclude的元素中呈现子内容。

指令

代码语言:javascript
复制
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>'
    };
});
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31010762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档