Per me, questo sembra il modo più semplice per farlo:
Code Pen
HTML
<html ng-app='app'>
<body ng-controller='MainController as vm'>
<input ng-repeat='thing in vm.things'>
<hr />
<button ng-click='vm.addThing()'>Add Thing</button>
</body>
</html>
JS
angular
.module('app', [])
.controller('MainController', MainController)
;
function MainController($timeout) {
var vm = this;
vm.things = [{}];
vm.addThing = function() {
vm.things.push({});
$timeout(function() {
// have to do this in a $timemout because
// we want it to happen after the view is updated
// with the newly added input
angular
.element(document.querySelectorAll('input'))
.eq(-1)[0]
.focus()
;
}, 0);
};
}
Personalmente, mi sarebbe effettivamente utilizzare jQuery e rendere il codice ancora più semplice:
$('input:last').focus();
Invece di:
angular
.element(document.querySelectorAll('input'))
.eq(-1)[0]
.focus()
;
fonte
2015-09-07 20:33:48