Auto focus in ng-repeat in angularjs

前端 未结 4 1947
遇见更好的自我
遇见更好的自我 2021-01-17 19:04

I uses ng-repeat to get multiple phone numbers

4条回答
  •  死守一世寂寞
    2021-01-17 19:12

    What happens is you have a list of inputs that is demanding the focus when the page loads at the same time. Since the last input renders last, it always gets the autofocus.

    The solution would be to apply the autofocus attribute when needed.

    Add Phone

    Controller:

    $scope.addPhone = function() {
        $scope.phones[$scope.phones.length-1].autofocus = 'false'; // unfocus the old
        $scope.phones.add('');
        $scope.phones[$scope.phones.length-1].autofocus = 'true'; // focus the new
    }
    

提交回复
热议问题