Я исправил это с помощью событий, таких как фокус и размытия.
См. Решение here.
myApp.directive('focusComponent', ['uiGridConstants', 'uiGridEditConstants',
function(uiGridConstants, uiGridEditConstants) {
return {
require: ['?^uiGrid', '?^uiGridRenderContainer'],
scope: true,
restrict: 'E',
template: '<div><div>Some text</div><input type="text"></div>',
link: function($scope, element, attrs, controllers) {
console.log(element);
console.log(arguments);
setTimeout(function() {
angular.element($(element).children().children()[1])[0].focus();
angular.element($(element).children().children()[1])[0].select();
}, 10);
var uiGridCtrl = controllers[0];
var renderContainerCtrl = controllers[1];
//set focus at start of edit
$scope.$on(uiGridEditConstants.events.BEGIN_CELL_EDIT, function() {
var thisEle = angular.element($(element).children().children()[1])[0];
thisEle.focus();
thisEle.select();
thisEle.style.width = (thisEle.parentElement.offsetWidth - 1) + 'px';
element.on('blur', function(evt) {
console.log("blur - element");
$scope.stopEdit(evt);
});
});
$scope.stopEdit = function(evt) {
// no need to validate a dropdown - invalid values shouldn't be
// available in the list
$scope.$emit(uiGridEditConstants.events.END_CELL_EDIT);
};
element.on('keydown', function(evt) {
switch (evt.keyCode) {
case uiGridConstants.keymap.ESC:
evt.stopPropagation();
$scope.$emit(uiGridEditConstants.events.CANCEL_CELL_EDIT);
break;
}
if (uiGridCtrl && uiGridCtrl.grid.api.cellNav) {
evt.uiGridTargetRenderContainerId = renderContainerCtrl.containerId;
if (uiGridCtrl.cellNav.handleKeyDown(evt) !== null) {
$scope.stopEdit(evt);
}
} else {
//handle enter and tab for editing not using cellNav
switch (evt.keyCode) {
case uiGridConstants.keymap.ENTER: // Enter (Leave Field)
case uiGridConstants.keymap.TAB:
evt.stopPropagation();
evt.preventDefault();
$scope.stopEdit(evt);
break;
}
}
return true;
});
}
}
}
]);
Вопрос касается плагина AngularJS под названием UI Grid. Как ваш ответ уместен? –