Один из подходов состоит в том, чтобы сохранить элемент, имеющий фокус:
<input ng-model="x" save-focus="lastTarget=$target">
app.directive("saveFocus", function() {
return { link: postLink, };
function postLink(scope, elem, attrs) {
elem.on("focus", function (e) {
scope.$apply(function() {
scope.$eval(attrs.saveFocus, {$target:e.target});
});
});
}
})
Затем нажмите элемент, чтобы изменить фокус обратно:
<button ng-click="x=+x+1" re-focus="lastTarget">
app.directive("reFocus", function() {
return { link: postLink, };
function postLink(scope, elem, attrs) {
elem.on("click", function(e) {
scope.$eval(attrs.reFocus).focus();
})
}
})
angular.module("app",[])
.directive("saveFocus", function() {
return { link: postLink, };
function postLink(scope, elem, attrs) {
elem.on("focus", function (e) {
scope.$apply(function() {
scope.$eval(attrs.saveFocus, {$target:e.target});
});
});
}
})
.directive("reFocus", function() {
return { link: postLink, };
function postLink(scope, elem, attrs) {
elem.on("click", function(e) {
scope.$eval(attrs.reFocus).focus();
})
}
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<input ng-model="x" save-focus="lastTarget=$target">
<br>
<button ng-click="x=+x+1" re-focus="lastTarget">
Increment X
</button>
</body>