角度-将焦点放在动态创建的输入字段上 [英] Angular - Give focus to a dynamically created input field

查看:63
本文介绍了角度-将焦点放在动态创建的输入字段上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将焦点添加到新创建的字段? 请参阅到目前为止的示例: http://jsfiddle.net/aERwc/165/

How would I add focus to the newly created field? See example thus far: http://jsfiddle.net/aERwc/165/

$scope.addField = function() {console.log('hi');
    $scope.fields[$scope.keyToAdd] = $scope.valueToAdd;
    $scope.setFieldKeys();
    $scope.keyToAdd = '';
    $scope.valueToAdd = '';
}

推荐答案

您可以使用此方法,但是需要将动画添加到ng-repeat中.参见 ng-repeat动画完整回调

You can use this approach however it will require to add animation to your ng-repeat. see ng-repeat animation complete callback

基本上在回调调用element.focus()

.animation('.repeat-animate', function () {
  return {
    enter: function (element, done) {
      element.hide().show(100, function(){
        var scope = element.scope();
        scope.$evalAsync(function(){ 
          element.find(':last')[0].focus();
        }); 
      });
    }
  };
});

更新的CODEPEN: http://codepen.io/ev-tt/pen/BNXBmd?editors = 101

UPDATED CODEPEN: http://codepen.io/ev-tt/pen/BNXBmd?editors=101

这篇关于角度-将焦点放在动态创建的输入字段上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆