我在代码中使用 ionic 弹出窗口,其中创建了一个指令来使自动光标聚焦输入框。第一次打开弹出窗口时它工作正常,但光标没有聚焦到当我关闭弹出窗口并再次打开它时,输入框再次出现,任何人都可以告诉我为什么会发生这种情况。
代码:
$scope.data = {};
var myPopup = $ionicPopup.show({
template: '<input focus-me type="text" ng-model="data.expensetype" limit-char limit="15">',
title: $translate.instant('{{"pentercoconuttype_message" | translate}}'),
scope: $scope,
buttons: [
{ text: $translate.instant('{{"pcancel_message" | translate}}') },
{
text: $translate.instant('{{"psave_message" | translate}}'),
type: 'button-positive',
onTap: function (e) {
if (!$scope.data.expensetype) {
//don't allow the user to close unless he enters producttype
e.preventDefault();
} else {
addExpenseCategory();
return $scope.data.expensetype;
}
}
},
]
});
myPopup.then(function (res) {
$log.log('Tapped!', res);
myPopup.close();
});
指令:
.directive('focusMe', function($timeout) {
return {
link: function(scope, element, attrs) {
$timeout(function() {
element[0].focus();
});
}
};
});
最佳答案
只需在 $timeout 中添加 150 毫秒即可确保 View 完全渲染。
.directive('focusMe', function($timeout) {
return {
link: function(scope, element, attrs) {
$timeout(function() {
element[0].focus();
}, 150);
}
};
});
关于javascript - 选择一次后,Ionicpopup 自动光标就会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46117437/