模板是带有2个标签的“标签”。 在第一个选项卡上,我有一个我想要关注的input字段。 当应用程序以input字段聚焦开始时它正在工作,但当我切换到第二个选项卡然后再返回到第一个选项卡时,输入失去焦点。
当我从标签2转到1时,我希望它成为焦点。 当我在元素外部单击时,它也会失去焦点。 我实际上希望input字段始终是重点。
我试过一些已经提到的解决方案
.directive('focusMe', function($timeout) { return { scope: { trigger: '=focusMe' }, link: function(scope, element) { scope.$watch('trigger', function(value) { if(value === true) { //console.log('trigger',value); //$timeout(function() { element[0].focus(); scope.trigger = true; //}); } }); } }; }); <label class="item item-input" focus-me> <input type="number" class="somett" ng-model="code" ng-model-options="{ debounce: 100 }" placeholder="Ready" ng-change="load(code) " focus-on="focusTextInput" autofocus> </label>Template is 'tabs' with 2 tabs. On the first tab, I have an input field I want to put focus on. It is working when the app starts with the input field being focused, but when I switch to the second tab and then back to the first tab, the input loses focus.
I would like it be focused when I go from tab 2 to 1 too. When I click outside the element, it to loses focus as well. I actually want that input field to be ALWAYS focused.
I tried some already mentioned solutions:
.directive('focusMe', function($timeout) { return { scope: { trigger: '=focusMe' }, link: function(scope, element) { scope.$watch('trigger', function(value) { if(value === true) { //console.log('trigger',value); //$timeout(function() { element[0].focus(); scope.trigger = true; //}); } }); } }; }); <label class="item item-input" focus-me> <input type="number" class="somett" ng-model="code" ng-model-options="{ debounce: 100 }" placeholder="Ready" ng-change="load(code) " focus-on="focusTextInput" autofocus> </label>最满意答案
您可以使用View LifeCycle和Events 。
将其添加到您的控制器。
$scope.$on('$ionicView.enter', function() { $scope.$broadcast("focusTextInput"); });You can use View LifeCycle and Events.
Add this to your controller.
$scope.$on('$ionicView.enter', function() { $scope.$broadcast("focusTextInput"); });更多推荐
发布评论