import angular = require('angular'); /* @ngInject */ export class SetFocus implements angular.IDirective { private $log: angular.ILogService; publicstaticFactory(): angular.IDirectiveFactory { return () = >newFocus(); } publiclink:angular.IDirectiveLinkFn = (scopeS: angular.IScope, elementS: JQuery, attrsS: angular.IAttributes) => { if (!this.$log) { this.$log=angular.injector(['ng']).get('$log'); } if (attrsS.focus==='true') { elementS[0].focus() ; } scopeS.$on(attrsS.focus, (e) => { elementS[0].focus(); }); } }; // Remove on Angular 2 require('../../operations.module') .directive('setFocus', () => new SetFocus());
$scope.addNewItem = function () {
/* stuff here to add a new item... */
$scope.$broadcast('newItemAdded');
};
<input type="text" focus-on="newItemAdded" />