autofocus
似乎是问题所在。它会立即启动模糊事件并导致再次隐藏输入字段。在输入可见之后尝试手动将焦点放在输入字段上(使用$ timeout)。见http://jsfiddle.net/awqtsLpy/。我将edit
函数移动到了directive.link部分以访问该元素。
app.directive('emailRecipients', function ($timeout) {
return {
restrict: 'E',
controller: function ($scope) {
$scope.focus = false;
$scope.addressees = [];
var i = 10;
while (i) {
$scope.addressees.push({
"name": i
});
i--;
}
},
link: function ($scope, element) {
$scope.click = function() {
$scope.focus = !$scope.focus;
$scope.value = null;
}
$scope.deleteFromInput = function ($event) {
$event.stopPropagation();
}
$scope.edit = function ($event, addressee) {
$event.stopPropagation();
$scope.value = addressee;
addressee.editable = true;
$scope.focus = true;
$timeout(function() {
element.find('input').focus();
});
}
}
}
});
$ timeout也适用于我的版本。我试过了scope。$ apply和setTimeout之前,这些都不起作用。 :) – marcel