在使用AngularJS创buildinput之后着重于input

我一直在用AngularJS做一个待办事项列表,想知道在创build一个input框后,是否有一种方法可以通过点击一个button来创build。

实际上,我在控制器中的保存function是这样定义的:

$scope.save = function() { $scope.objetivo.$save() .then(function() { $scope.message = { text : 'Saved' }; $scope.objective = new Objective(); }) .catch(function(err) { console.log(err.data); if(err.data.code === 11000) { text = 'This objective is already registered' } $scope.message = { text : text || "Error when saving" }; }); }; 

我认为可能是通过添加input框然后专注于它,但我不知道如何去做。

这个小提琴演示如何实现焦点元素,即使在asynchronous逻辑。

视图

 <div ng-controller="MyCtrl"> <button ng-click="someAsyncFunction()"> Set focus on async functions </button> <input type="text" set-focus="{{setFocus}}"> </div> 

AngularJS应用程序

 var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl', function ($scope, $timeout) { $scope.someAsyncFunction = function () //the following timeout indicates ansyc functions. $timeout(function () { $scope.setFocus = true; }, 250); } }); myApp.directive('setFocus', function($timeout) { return { scope: { trigger: '@setFocus' }, link: function(scope, element) { scope.$watch('trigger', function(value) { if(value === "true") { $timeout(function() { element[0].focus(); },250); } }); } }; });