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