javascript - 如何在模板的输入中使用 Angular 的 ngFocus 指令(或嵌入在 Ionic Framework 中的任何指令)向元素添加类?

标签 javascript angularjs ionic-framework

这个问题在这里已经有了答案:





Angular.js: Set CSS when Input is on Focus

(3 个回答)


5年前关闭。




我有一个这样的模板:

    <label class="item item-input"
           ng-class="{'focus':authData.username.focus}"> <--add class here if input:focus
        <span class="input-label">Username</span>
        <input type="text"
               name="username"
               ng-model="authData.username"
               ng-focus="" <--check if focus
               required ng-minlength="5" ng-maxlength="20">
    </label>

如何检查输入是否具有焦点并将类添加到标签? Controller 中没有外部功能是否可能?

最佳答案

<label class="item item-input"
           ng-class="{'focus':focus===true}">
        <span class="input-label">Username</span>
        <input type="text"
               name="username"
               ng-model="authData.username"
               ng-focus="focus=true" ng-blur="focus=false"
               required ng-minlength="5" ng-maxlength="20">
    </label>

使用 ngFocus 和 ngBlur 指令来检测元素上的焦点变化并更新 var focus 的值相应地为真/假。

关于javascript - 如何在模板的输入中使用 Angular 的 ngFocus 指令(或嵌入在 Ionic Framework 中的任何指令)向元素添加类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34822035/

相关文章:

javascript - 使用 DOM 引用下拉菜单中的选定项

javascript - 使用 Vue 和 Laravel 的神秘问题?

javascript - 在 ng-repeat 中获取行的索引(或 id)以对特定行执行一些操作

android - ionic 调用 Intent 不起作用

mysql - 将 ionic 连接到 mysql 数据库

javascript - 有没有更优雅的方式用javascript编写这个?

javascript - 通过 DOM 函数附加新元素,还是使用 HTML 标签附加字符串,哪个更好?

javascript - 使用 angularjs 的电话格式自定义指令

angularjs - 如何在浏览器端解密在nodejs加密库上加密的数据?

ionic-framework - 我怎样才能把 ionic 项目图标放在行尾