美文网首页
AngularJS input自动聚焦

AngularJS input自动聚焦

作者: 待花谢花开 | 来源:发表于2017-06-15 09:10 被阅读0次

当我们在使用ionic进行开发的时候,使用angularjs中的ng-focus指令并没有效果。
比如当我们在需要点击某个按钮然后弹出输入框,并且需要自动聚焦到这个输入框的时候。如果使用angularjs的ng-focus指令,这样是没有任何效果的。
这时候就需要我们重写focus服务。
<pre style="background-color: rgb(255, 255, 255); font-family: 宋体; font-size: 12pt;"><pre name="code" class="html"> .factory('focus', function ($timeout, $window) {
return function (id) {
$timeout(function () {
var element = $window.document.getElementById(id);
if (element)
element.focus();
});
};
})

然后给输入框设置id
<input id="inputFocus" ng-model="" type="text" placeholder="" />
最后在input对应的controller中注入focus
通过focus('inputFocus')对input进行自动聚焦。

相关文章

网友评论

      本文标题:AngularJS input自动聚焦

      本文链接:https://www.haomeiwen.com/subject/eldyqxtx.html