加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 使用ng-class加载指令时类指令不起作用

发布时间:2020-12-17 18:03:24 所属栏目:安全 来源:网络整理
导读:我正在尝试使用ng-class加载’class’指令.但是当我这样做时,我的指令永远不会被加载.该指令是一个多用途指令,我不想在此创建一个孤立的范围.它将仅在需要时加载,基于ng-class条件,因此不使用attribute或element指令.有没有人试过这样做并成功了? 使用 div
我正在尝试使用ng-class加载’class’指令.但是当我这样做时,我的指令永远不会被加载.该指令是一个多用途指令,我不想在此创建一个孤立的范围.它将仅在需要时加载,基于ng-class条件,因此不使用attribute或element指令.有没有人试过这样做并成功了?

使用< div ng-class =“someClass {{tooltip:enabled}}”>< / div>调用此指令
这里启用的是范围变量.

app.directive('tooltip',['$timeout','$location','$rootScope',function (timer,$location,$rootScope) {
    return {
        restrict: 'C',transclude: true,link: function (scope,element) {
            var printContent = function () {
                /*  uses the content of .tooltip-content if it is a complex html tooltip,otherwise
                    you can use the title attribute for plaintext tooltips
                */
                var tooltipContent = $(element).find('.tooltip-content').html();
                if (!tooltipContent) {
                    tooltipContent = $(element).attr('title');
                }
                $(element).tooltip({
                    content: tooltipContent,items: "img,a,span,button,div",tooltipClass: "tooltip",position: { my: "left+30 top",at: "right top",collision: "flipfit" },show: { effect: "fadeIn",duration: "fast" },hide: { effect: "fadeOut",open: function (event,ui) { $rootScope.tooltipElement = event.target; }
                });
            };
            timer(printContent,0);
        }
    };
}]);

解决方法

有趣的问题.您似乎不想使用ng-class指令,因为在添加类之后不会重新编译内容.您可能希望创建自己的动态类指令,该指令在值为true时实际重新编译:

app.directive('dynamicClass',function($compile) {
    return {
        scope: {
            dynamicClassWhen: '=',dynamicClass: '='
        },link: function(scope,elt,attrs) {
            scope.$watch('dynamicClassWhen',function(val) {
                if (val) {
                    console.log(val);
                    elt.addClass(scope.dynamicClass);
                    $compile(elt)(scope);
                }
            });
        }
    };
});

您可能需要修改此功能才能删除该类,具体取决于$compile是否足以满足您的要求,或者您是否需要进一步操作html,但这似乎适合您.我用这个做了fiddle.

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读