角度指令模板和儿童指令包含

Rya*_*ann 2 javascript asp.net-mvc-4 angularjs angularjs-directive angularjs-ng-transclude

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

我遇到的问题是让我的父指令处理嵌套在其中的子指令有关父指令上的模板.

我想要这样的概念,

-Toolbar - >按钮组 - > - >按钮

所以我有三个指令

xyz-toolbar xyz-toolbar-button-group xyz-toolbar-button

工具栏指令仅限于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>
Run Code Online (Sandbox Code Playgroud)

现在我有一个kb-toolbar指令

    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;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

然后是按钮组

    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>'
    };
});
Run Code Online (Sandbox Code Playgroud)

最后是按钮

    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>'
    }
});
Run Code Online (Sandbox Code Playgroud)

所以基本问题是kb-toolbar-button-group呈现无序列表,但不包含子节点.所以我需要在该指令的模板""上添加一些内容,使其在其中包含kb-toolbar-button.

Pan*_*kar 5

您可以通过使用transcludeinside 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>'
    };
});
Run Code Online (Sandbox Code Playgroud)