简体   繁体   English

将伪类添加到角度的自定义模板/指令中

[英]Add pseudo class to a custom template / directive in angular

I am working on a directive that creates a custom dropdown menu. 我正在创建一个自定义下拉菜单的指令。 A regular HTML select has a pseudo class active (eg, .mydropdown:active) that can be styled using css. 常规HTML选择具有可使用CSS设置样式的伪类活动类(例如.mydropdown:active)。 The select remains active while the dropdown menu is open. 下拉菜单打开时,选择保持激活状态。 I would like to add this functionality to my directive. 我想将此功能添加到我的指令中。 My template is based on buttons. 我的模板基于按钮。 The default behavior of buttons is to become active when clicked (mouse down) so while the dropdown is open they are no longer active. 按钮的默认行为是在单击(鼠标向下)时变为活动状态,因此当下拉列表处于打开状态时,它们将不再处于活动状态。

Here is what a drop down looks like when it is active: 下拉菜单处于活动状态时的外观如下: 基于:drop伪类的样式

Here is what mine looks like when I interact with it and the button is no longer active. 这是我与之互动时的样子,并且该按钮不再处于活动状态。 I want the button to remain active until the dropdown is closed: 我希望按钮在下拉列表关闭之前保持活动状态: 在此处输入图片说明

Here is the code for my directive: 这是我的指令的代码:

var app_dropdowns = angular.module('dropdown.directives', ['offClick']);

app_dropdowns.directive('dropdownMultiselect', function() {
    return {
        restrict: 'E',
        scope: {
            model: '=',
            options: '=',
            labelname: '@labelname',
            pre_selected: '=preSelected'
        },
        template: "<div class='btn-group' data-ng-class='{open: open}' off-click='open=false'>" +
            "<button class='btn dropdown-toggle dropselect' active='true' data-ng-click='openDropdown();'>{{labelname}}<span class='caret' style = 'margin-left: 20px;'></span></button>" + "<ul class='dropdown-menu' style = 'min-width: 300px;' aria-labelledby='dropdownMenu'>" + "<li><a data-ng-click='selectAll()'><i class='fa fa-check-square-o'></i>  Select All</a></li>" + "<li><a data-ng-click='deselectAll();'><i class='fa fa-square-o'></i>  Select None</a></li>" + "<li class='divider'></li>" + "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>{{option.name}}<span data-ng-class='isChecked(option._id)'></span></a></li>" + "</ul>" + "</div>",
        controller: function($scope) {

            $scope.model = [];
            $scope.options = [];
            $scope.openDropdown = function() {
            console.log($scope.options.length)
        if (!$scope.open && $scope.options.length > 0){
            $scope.open = true;
        } else {
            $scope.open = false;
        }

            };

            $scope.selectAll = function() {
                $scope.model = [];
                angular.forEach($scope.options, function(item) {
                    $scope.model.push(item);
                });
                console.log($scope.model);
            };
            $scope.deselectAll = function() {
                $scope.model = [];
                console.log($scope.model);
            };
            $scope.setSelectedItem = function() {
                var _id = this.option._id;
                var index = -1;
                var i = 0;

                angular.forEach($scope.model, function(item) {
                    console.log(_id);
                    console.log(item._id);
                    if (item._id == _id) {
                        index = i
                    }
                    i = i + 1;
                });
                if (index > -1) {
                    $scope.model.splice(index, 1);
                } else {

                    $scope.model.push(this.option);
                    console.log($scope.model);
                }

                return false;
            };
            $scope.isChecked = function(_id) {
                var index = -1;
                var i = 0;
                angular.forEach($scope.model, function(item) {
                    if (item._id == _id) {
                        index = i
                    }
                    i = i + 1;
                });
                if (index > -1) {
                    return 'fa fa-check pull-right';
                }
                return false;
            };
        }
    }
});

I am creating css for the dropselect class: 我正在为dropselect类创建CSS:

.dropselect,
.dropselect:hover
 {
    height: 45px;
    background-color: #ffffff;
    border: 2px solid #dce4ec;
    border-radius: 4px;
    color: #2c3e50;
    font-size: 15px;
    line-height: 1.42857143;
    padding: 10px 15px;
    margin: 0;
    position: relative;
    display: inline-block;
    vertical-align: middle;
}

.dropselect:active,
.dropselect:focus {
    height: 45px;
    background-color: #ffffff;
    border: 2px solid black;
    border-radius: 4px;
    color: #2c3e50;
    font-size: 15px;
    line-height: 1.42857143;
    padding: 10px 15px;
    margin: 0;
    position: relative;
    display: inline-block;
    vertical-align: middle;
}

Here is a fiddle to further explain: https://jsfiddle.net/krd3y6dx/7/ 这是一个进一步解释的小提琴: https : //jsfiddle.net/krd3y6dx/7/

I figured it out. 我想到了。 The trick is to add a new class that has the same css as the desired active state and then use ng-class to conditionally apply that class when the dropdown is open. 诀窍是添加一个具有与所需活动状态相同的CSS的新类,然后在下拉列表打开时使用ng-class有条件地应用该类。

Fiddle: https://jsfiddle.net/krd3y6dx/9/ 小提琴: https : //jsfiddle.net/krd3y6dx/9/

css: CSS:

.dropselect:active, .dropselect:focus, .dropactive {
    height: 45px;
    background-color: #ffffff;
    border: 2px solid black;
    border-radius: 4px;
    color: #2c3e50;
    font-size: 15px;
    line-height: 1.42857143;
    padding: 10px 15px;
    margin: 0;
    position: relative;
    display: inline-block;
    vertical-align: middle;
}

and html template: 和html模板:

template: "<div class='btn-group' data-ng-class='{open: open}'>" +
        "<button class='btn dropdown-toggle dropselect' ng-class='{dropactive: open}' data-ng-click='openDropdown();' ng-disabled = 'options.length == 0'>{{labelname}}<span class='caret' style = 'margin-left: 20px;'>...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM