使用键盘导航UI

时间:2013-05-31 14:44:28

标签: angularjs

考虑以下标记 -

<ul id="list">
    <li class="list-item" tabindex="0">test 1</li>
    <li class="list-item" tabindex="1">test 2</li>
    <li class="list-item" tabindex="2">test 3</li>
    <li class="list-item" tabindex="3">test 4</li>
    <li class="list-item" tabindex="4">test 5</li>
    <li class="list-item" tabindex="5">test 6</li>
    <li class="list-item" tabindex="6">test 7</li>
</ul>

和这段jQuery代码 -

$(".list-item").bind({
    keydown: function(e) {
        var key = e.keyCode;
        var target = $(e.currentTarget);

        switch(key) {
            case 38: // arrow up
                target.prev().focus();
                break;
            case 40: // arrow down
                target.next().focus();
                break;
        }
    },

    focusin: function(e) {
        $(e.currentTarget).addClass("selected");
    },

    focusout: function(e) {
        $(e.currentTarget).removeClass("selected");
    }
});
$("li").first().focus();

如何以角度方式移植此代码?到目前为止我有这个 -

 <li class="list-item" ng-repeat="item in items" tabindex="{{item.tabIndex}}">
                        {{item.name}}
                    </li>

如何以角度进行绑定?

4 个答案:

答案 0 :(得分:11)

我认为最好的方法是使用tabindex来定位焦点元素。试试这样的事情;

<li class="list-item" ng-repeat="item in items" 
    tabindex="{{item.tabIndex}}"
    ng-class="item.selected"
    ng-keydown="onKeydown(item, $event)" ng-focus="onFocus(item)">{{item.name}}
</li>

然后在你的控制器中你需要Keydown处理程序;

var KeyCodes = {
    BACKSPACE : 8,
    TABKEY : 9,
    RETURNKEY : 13,
    ESCAPE : 27,
    SPACEBAR : 32,
    LEFTARROW : 37,
    UPARROW : 38,
    RIGHTARROW : 39,
    DOWNARROW : 40,
};

$scope.onKeydown = function(item, $event) {
        var e = $event;
        var $target = $(e.target);
        var nextTab;
        switch (e.keyCode) {
            case KeyCodes.ESCAPE:
                $target.blur();
                break;
            case KeyCodes.UPARROW:
                nextTab = - 1;
                break;
            case KeyCodes.RETURNKEY: e.preventDefault();
            case KeyCodes.DOWNARROW:
                nextTab = 1;
                break;
        }
        if (nextTab != undefined) {
            // do this outside the current $digest cycle
            // focus the next element by tabindex
           $timeout(() => $('[tabindex=' + (parseInt($target.attr("tabindex")) + nextTab) + ']').focus());
        }
};

一个保持简单的焦点处理程序;

$scope.onFocus = function(item, $event) {
    // clear all other items
    angular.forEach(items, function(item) {
        item.selected = undefined;
    });

    // select this one
    item.selected = "selected";
};

这是我的头脑,如果它像我一样帮助任何人遇到这个帖子。

答案 1 :(得分:9)

您还可以创建一个如下所示的角度指令:

claimApp.directive('keyNavigation', function ($timeout) {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 38) {
                var target = $(event.target).prev();
                $(target).trigger('focus');
            }
            if (event.which === 40) {
                var target = $(event.target).next();
                $(target).trigger('focus');
            }
        });
    };
});

然后在HTML中使用它:

<li class="list-item" ng-repeat="item in items" key-navigation>
                    {{item.name}}
                </li>

答案 2 :(得分:4)

这个问题在提出问题前几个月得到了解答,并在Plunker有一个实际工作示例。

var app = angular.module('test', []);

app.controller('testCtrl',function ($scope, $window) {
$scope.console = $window.console;

$scope.records = [];
for (var i = 1; i <= 9; i++) {
$scope.records.push({ id: i, navIndex: i, name: 'record ' + i});
}

$scope.focusIndex = 3;

$scope.open = function ( index ) {
var record = $scope.shownRecords[ index ]
console.log('opening : ', record );
 };

$scope.keys = [];
$scope.keys.push({ code: 13, action: function() { $scope.open( $scope.focusIndex ); }});
$scope.keys.push({ code: 38, action: function() { $scope.focusIndex--; }});
$scope.keys.push({ code: 40, action: function() { $scope.focusIndex++; }});

在此主题为Stackoverflow

之前几周,类似问题的链接就出现了问题

答案 3 :(得分:1)

没有jQuery的指令

.directive('arrow', function ($timeout) {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if (event.which === 38) {
              var prevNode = element[0].previousElementSibling;
              var prev = angular.element(prevNode);
              prev[0].focus();
            }
            if (event.which === 40) {
                var target = element.next();
                target[0].focus();
            }
        });
    };
});

html中的用法

<tr arrow>
  <td>test 1</td>                       
</tr>
<tr arrow>
  <td>test 2</td>                       
</tr>
相关问题