css - 根据他的内容更改父 li 元素类

标签 css angularjs

我有这个简单的无序列表

<ul>
    <li>
        <p>Entertainment</p>
        <ul>
            <li><p>Lady GAGA</p></li>
            <li><p>Lady GAGA</p></li>
            <li><p>Lady GAGA</p></li>
            <li><p>Lady GAGA</p></li>
        </ul>
    </li>

    <li>
        <p>Fasion</p>
    </li>

    <li>
        <p>Animals</p>
    </li>
</ul>

我想要 <li><ul>它的内部会自动获得一个特殊的类/样式,因此如果内容(ul)将被移动或删除,特殊类也会从父级 <li> 中删除。 .

如果它可以在 css 或 sass 中完成但在 ng-class 中,我会很高兴也可以。

谢谢。

最佳答案

您可以使用 directive 来做到这一点.

angular.module('myModule').directive('li', function() {
    return {
        restrict: 'E',
        link: function(scope, element) {
            scope.$watch(function() {
                return $(element).find("ul").length;
            }, function(newValue) {
                if(newValue === 0) {
                    element.removeClass("hasSubList");
                }
                else {
                    element.addClass("hasSubList");
                }
            });
        }
    };
});

关于css - 根据他的内容更改父 li 元素类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844664/

相关文章:

html - Flexbox,div 之外的卡片和文本之间的空间

javascript - 如何在不使用 !important 的情况下覆盖 primefaces 组件内联样式?

javascript - Angular - 在 ng-repeat 中使用 orderBy 以升序排列

javascript - 使用 Javascript/jQuery 隐藏/删除内容

html - 在 bootstrap 主题中添加向右拉按钮

css - 向 XSL 添加 ID 或类

javascript - Angular : factory and controller

javascript - 使用 ui-sref 时不会自动生成 href

javascript - AngularJS 指令双向绑定(bind)在 Ionic 中不起作用

javascript - Angular - CDN 引用作品,本地 min 文件没有