【问题标题】:How can I use the element tags from other custom directive as the template for another custom directive如何使用来自其他自定义指令的元素标签作为另一个自定义指令的模板
【发布时间】:2016-08-24 20:05:54
【问题描述】:

我对 angularjs 比较陌生。我正在客户端使用 angularjs 创建一个 Web 应用程序。我已经制定了几个自定义指令,并希望客户能够自定义他们每次登录时看到的指令。我试图做的是创建一个字符串,其中填充了用户选择的自定义指令标签。此信息将作为 cookie 值保存,并在用户登录时由控制器加载。

这是传递过来的 cookie 值(我现在已经硬编码了):

public usersCustoms = `<div class="mdl-grid"> 
                                <div id="firstrow" class="mdl-cell mdl-cell--12-col mdl-cell--8-col-tablet mdl-grid" >
                                    <cit-recap > </cit-recap>
                                    <veh-ar-recap></veh-ar-recap>
                                    <new-models-inv></new-models-inv>
                                    <used-models-inv></used-models-inv>
                                </div>
                                <div id="secondrow" class="mdl-cell mdl-cell--12-col mdl-cell--8-col-tablet mdl-grid">
                                    <deal-recap></deal-recap>
                                    <vehicle-inv></vehicle-inv>
                                    <acct-trend></acct-trend>
                                </div>
                                <div id="thirdrow" class="mdl-cell mdl-cell--12-col mdl-cell--8-col-tablet mdl-grid">
                                    <parts-inv></parts-inv>
                                </div>
                            </div>`;

这是指令,最后一个指令“usersCustomDirective”是应该拉入其他指令的指令。

namespace DashboardDemo {
function citRecap(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/citrecap.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('citRecap', citRecap);


function vehArRecap(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/vehArRecap.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('vehArRecap', vehArRecap);

function newModelsInv(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/newModelsInv.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('newModelsInv', newModelsInv);

function usedModelsInv(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/usedModelsInv.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('usedModelsInv', usedModelsInv);



function dealRecap(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/dealRecap.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('dealRecap', dealRecap);


function vehicleInv(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/vehicleInv.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('vehicleInv', vehicleInv);


function acctTrend(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/acctngTrend.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('acctTrend', acctTrend);

function partsInv(): ng.IDirective {
    return {
        templateUrl: 'ngDashboardDemo/Views/Directives/partsinv.html',
        restrict: 'AE',
        replace: true,
        controller: DashboardDemo.DashboardController,
        controllerAs: 'dc',
        link: function (scope, elem, attrs) {
        }
    }

}
angular.module('DashboardDemo').directive('partsInv', partsInv);

function usersCustomDirective(): ng.IDirective {
    return {

        link: function (scope, elem, attrs) {
            //elem.html(scope.dc.usersCustoms);
            //console.log(scope.dc.usersCustoms);
            elem.html(scope.userOptions);

        },
        restrict: 'AEC',
        replace: true,
        transclude: true,
        controller: DashboardController,
        controllerAs: 'dc',
        scope: {
            userOptions: '@userOptions'
        },
        //template: scope.userOptions

    }
}
angular.module('DashboardDemo').directive('usersCustomDirective', usersCustomDirective);

}

这是我的视图 HTML:

<main class="mdl-color--grey-100">
<users-custom-directive user-options ="{{dc.usersCustoms}}"></users-custom-directive></main>

正如它现在所写的那样,我假设的 html 没有显示,因为 html 无法识别我的自定义标签。我也尝试在链接函数中使用 elem.replaceAll ,但得到了类似的结果。当我在 chrome 中打开开发人员工具时,在元素下我看到了我的 div 和我在 div 中的自定义标签,但是,只有 div 被渲染,自定义标签没有。控制台记录了传入的字符串值。我还尝试将“usersCustomDirective”更改为:

function usersCustomDirective(): ng.IDirective {
    return {

        link: function (scope, elem, attrs) {
            //elem.html(scope.dc.usersCustoms);
            //console.log(scope.dc.usersCustoms);
            console.log(scope.userOptions);

        },
        restrict: 'AEC',
        replace: true,
        transclude: true,
        controller: DashboardController,
        controllerAs: 'dc',
        scope: {
            userOptions: '@userOptions'
        },
        template: scope.userOptions

    }
}

它不会在 html 中显示任何内容,而是显示我正在传递的文本字符串。

【问题讨论】:

    标签: javascript html angularjs typescript


    【解决方案1】:

    好的,我知道我需要做什么了。经过进一步研究,我需要在过程中包含 $compile 。我所做的更改是将 $compile 添加到指令函数调用中。然后我为模板分配了 scope.userOptions 值。我在模板上调用了 $compile 和 viola angularjs 魔术。现在效果很好。希望这对其他人有帮助。仅供参考,如果您像我一样使用打字稿并且收到错误消息:“IScope 类型上不存在属性空白”,您需要创建自己的接口并扩展 IScope 以包含该属性

    function usersCustomDirective($compile): ng.IDirective {
        return {
    
            link: function (scope: IMyScope, elem, attrs) {
                var template = scope.userOptions;
                var linkFn = $compile(template);
                var content = linkFn(scope);
                elem.append(content);
    
            },
            restrict: 'AEC',
            replace: true,
            controller: DashboardController,
            controllerAs: 'dc',
            scope: {
                userOptions: '@userOptions'
            },
    
        }
    }
    angular.module('DashboardDemo').directive('usersCustomDirective', usersCustomDirective);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多