100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > AngularJS 中 点击按钮 使焦点在指定文本框中

AngularJS 中 点击按钮 使焦点在指定文本框中

时间:2021-03-02 06:14:00

相关推荐

AngularJS 中 点击按钮 使焦点在指定文本框中

AngularJS 中 点击按钮 使焦点在指定文本框中

自定义指令

app.directive('focusMe', ['$timeout', '$parse', function ($timeout, $parse) {return {//scope: true, // optionally create a child scopelink: function (scope, element, attrs) {var model = $parse(attrs.focusMe);scope.$watch(model, function (value) {console.log('value=', value);if (value === true) {$timeout(function () {element[0].focus();});}});// to address @blesh's comment, set attribute value to 'false'// on blur event:element.bind('blur', function () {console.log('blur');scope.$apply(model.assign(scope, false));});}};}]);

html

Name: <input type="text" focus-me="isFocusedOn">focusOn: <button ng-click="setfocusOn()"></button>

js中

$scope.isFocusedOn=false;//是否将将焦点放在该处/** 放焦点*/$scope.setfocusOn=function(){$scope.isFocusedOn=true;}

参考:

How to set focus on input field? /questions/14833326/how-to-set-focus-on-input-field

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。