torbenrudgaard torbenrudgaard - 2 months ago 8
AngularJS Question

An easy way to set focus to a field in Angular

Is there an easy way to set focus to a field in Angular, or will I have to write a big

$scope.$watch.$element
script?

This is my page

<input type="text" ng-model="vm.agency" id="agency" aria-invalid="false">


This is my controller

if (!vm.agency) {
toaster.error({title: "MISSING AGENCY", body:"You must write the Agency name"});
vm.agency.focus();
return;
}

Answer Source

you can try this:-

if (!vm.agency) {
    toaster.error({title: "MISSING AGENCY", body:"You must write the Agency name"});
    angular.element("#agency").focus();
    return;
}