繁体   English   中英

更改元素onkeyup的类

[英]change class of element onkeyup

我正在使用angularjs应用程序。 这是一个用于手机的聊天应用程序。 它有两个模板

  1. userlist.html
  2. message.html

message.html模板中,一切正常,直到我按让( <input>标签)“输入”以键入内容为止。 键盘显示出来,并且它不会向上滑动视图。 当键盘可见时,它将显示旧消息。 因此为了向上滑动视图。 我正在考虑在标签的keyup上绑定一个事件,并更改CSS类以获得更新的视图。 因此,我创建了指令。 但是问题是当我在<input>标签上添加指令时,我只能访问input标签的元素。 我想访问指令中模板的完整DOM元素。 怎么做?

message.html

<div class="chat-sidebar-content">   element
<div class="chat-sidebar-chat chat-sidebar-panel2" id="chat-sidebar-user-1"> //I want to change class of this 

</div>

<footer class="chat-sidebar-footer form-group">
    <input class="form-control input-dark fs-mini" my-directive type="text"  placeholder="Type your message">    //added the directive here
</footer>
</div>

指令

angular.module('testApp').directive('myDirective', function() {


return function(scope, element, attr){

    console.log(element);

    element.bind("keyup", function(){

        console.log('keyup');    //this works
    });
};

});

这是您可以做什么的示例:

在视图中:

<input class="form-control input-dark fs-mini" ng-keyup="checkSomething()" type="text"  placeholder="Type your message">

在控制器中:

$scope.condition;
$scope.checkSomething = function (){
    $scope.condition = true;
};

在视图中,再次在相同范围(控制器)中的任何元素上:

<div ng-class="condition ? 'class-1' : 'class-2' " id="chat-sidebar-user-1">

这只是一个例子,如果您$scope.condition = true;检查,请随意放入$scope.condition = true; 直接在ng-keyup指令中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM