javascript - Angular Directive(指令)重新获得焦点

标签 javascript angularjs

我有一个简单的 TODO 应用程序(来自 Apress 书,Pro Angular)。目前我有一个小应用程序,用户可以在其中将一些信息键入一个框中,单击一个按钮,然后一个 TODO 任务就会弹出到屏幕上。我希望发生的是在用户单击按钮将任务添加到页面后,焦点再次转到输入文本框。我目前使用的是什么,但我想将其作为指令,以便我可以重用它。

JS

 var model = {
            user: "Adam"
        }
        var todoApp = angular.module('todoApp',[]);
        //get data
        todoApp.run(function($http){
            $http.get('todo.json').success(function(data){
                model.items = data;
            });
        });
        //need a directive that can listen for a button click
        todoApp.directive('refocus',function(){
            return {
                restrict: 'a',
                link: function(scope, elem, attrs){
                    scope.$watch('someEvent',function(){
                        //how to listen for a specific click event
                        //and return focus to the input element afterwards
                    })
                }
            }
        })
        todoApp.filter('checkedItems',function(){
            return function(items,showComplete){
                var resultArr=[];
                angular.forEach(items,function(item){
                    if(item.done==false || showComplete ==true){
                        resultArr.push(item);
                    }

                })
                return resultArr;
            }
        })
        todoApp.controller('ToDoCtrl',function($scope){
            $scope.todo = model;
            $scope.incompleteCount = function(){
                var count =0;
                angular.forEach($scope.todo.items,function(item){
                    if(!item.done){
                        count++;
                    }
                });
                return count;
            }

            $scope.addNewItem = function(actionText){
                $scope.todo.items.push({action: actionText, done: false});
                $scope.actionText = "";
                $scope.returnFocus('#getFocus');

            };
            $scope.returnFocus = function(elem){
                $(elem).focus();
            };
        });

HTML

<body ng-controller="ToDoCtrl">
<div class="page-header">
    <h1>
        {{todo.user}}'s To Do List
<span class="label label-default">

</span>
    </h1>
</div>
<div class="panel">
    <div class="input-group">
        <input id="getFocus" ng-model="actionText" class="form-control" ng-focus="" />
<span class="input-group-btn">
<button ng-click="addNewItem(actionText)" class="btn btn-default">Add</button>
</span>
    </div>
    <table class="table table-striped">
        <thead>
        <tr>
            <th>Description</th>
            <th>Done</th>
        </tr>
        </thead>
        <tbody>

            <tr ng-repeat="item in todo.items | checkedItems: showComplete | orderBy:'done'">
                <td>{{item.action}}</td>
                <td><input type="checkbox" ng-model="item.done"/></td>
            </tr>
        </tbody>
    </table>
    <div class="checkbox-inline">
        <label><input type="checkbox" ng-model="showComplete">Show complete</label>
    </div>
</div>
</body>

您可以在 addNewItem 函数中看到我使用 jQuery 选择器将焦点返回到输入文本,但这并不能使其可重用。我怎样才能完成我的 stub 指令以使这种类型的行为可重用?

最佳答案

有几个模块可以做到这一点。 (无耻的插头!)这是一个:angular-focus-on

我的方法是通过使用事件。它非常简单明了。这是一个fiddle实现了模块。

快速查看我更改的行:

首先将模块添加到您的应用中:

var todoApp = angular.module('todoApp',['kf.focusOn']);

然后我们通过添加 focus-onevent="todoAdded" 属性来使用它。因此,当触发 todoAdded 事件时,该元素将获得焦点。

<input ng-model="actionText" class="form-control" focus-on event="todoAdded" />

现在在 Controller 中,我们所要做的就是 $scope.$broadcast('todoAdded') 以触发 todoAdded 事件。看起来像:

$scope.addNewItem = function(actionText){
    $scope.$broadcast('todoAdded'); // this fires an event.
    $scope.todo.items.push({action: actionText, done: false});
    $scope.actionText = "";
};

现在让我们来看看这个模块是如何工作的:

angular.module('kf.focusOn', [])

.directive('focusOn', [function() {
    return {
        restrict: 'A', // Restrict to attribute only
        scope: {
            event: '@' // Accept an event attribute.
        },
        link: function($scope, elem) {
            $scope.$on($scope.event, function() { // On the event passed by $scope.event...
                elem[0].focus(); // Focus on the element.
            });
        }
    }
}]);

如您所见,它没有什么花哨的,而且非常简单,它接受一个 event 范围,并在事件触发时将焦点应用到元素。

关于javascript - Angular Directive(指令)重新获得焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24106812/

相关文章:

javascript - 通过链式服务传递 header

javascript - 如何使用对象数组创建对象

javascript - 页面重新加载后显示警报消息

javascript - 为什么我的 Controller 没有在下面的代码中注册?

javascript - 按键上的 Angular js 文本 Angular 组件事件

javascript - 在 JavaScript 中转换为字符串

javascript - jquery 中 .add() 的等效 javascript 函数是什么?

javascript - 获取 tr 元素的正确顺序作为 jQuery 对象

AngularJs UI-Router - 页面刷新 $state.current 现在是空的

javascript - WebPack:需要将 javascript 文件置于全局范围内