简体   繁体   English

如何使用AngularJS中具有ID值的过滤器从用户列表中搜索用户

[英]How to search for a user from a userslist using filter with ID value in AngularJS

I am working with a chat application. 我正在使用聊天应用程序。 I need to put an input search box for the user to search for a friend from his friends list. 我需要输入一个搜索框供用户从他的朋友列表中搜索一个朋友。 I have written code to get ID values and and trigger it onkeyup. 我已经编写了获取ID值并在onkeyup上触发它的代码。 However, I am getting an uncaught reference error of function(name) is undefined. 但是,我收到未定义的function(name)的引用错误。 Here I am getting users list from server side using socket code. 在这里,我正在使用套接字代码从服务器端获取用户列表。

Function in JS file JS文件中的功能

 $scope.searchFn = function () {
                var input, filter, ul, li;
                input = document.getElementById("input");
                //                filter = input.value.toUpperCase();
                ul = document.getElementById("list");
                li = ul.getElementsByTagName("li");
                for (i = 0; i < li.length; i++) {
                    a = li[i].getElementsByTagName("a")[0];
                    if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
                        li[i].style.display = "";
                    } else {
                        li[i].style.display =
                            "none";
                    }
                }
            }

HTML HTML

<main>
    <div class="container-fluid">
        <input type="hidden" id="user" value="<%= user.username %>" />
        <div class="row">
            <div class="col-sm-4">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <form action="/action_page.php" method="get">
                            <div class="input-group form-group">
                                <input id="input" class="form-control" name="browser" placeholder="Search friend.." ng-model="search" onkeypress="searchFn()">


                                <span class="input-group-btn">              
          <button class="btn btn-primary" type="button">
            <span class="glyphicon glyphicon-search"></span>
                                </button>
                                </span>

                            </div>
                        </form>
                        <h4 align="center">Friends</h4>
                    </div>
                    <div class="panel-body" id="scrl1">
                        <ul id="list"></ul>
                    </div>
                    <div class="panel-footer">
                        <p id="typing"></p>
                    </div>
                </div>
            </div>
            <div class=".space"></div>
            <div class="col-sm-8">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <h4 id="frndName" align="center">Chat Room</h4>
                        <a ng-click="clearchat()">
                            <span class="glyphicon glyphicon-trash" style="float:right;margin-top:-4%"></span>
                        </a>
                    </div>
                    <div class="panel-body" id="scrl2">
                        <p id="loading" align="center">Loading.....</p>
                        <p id="noChat" align="center">No More Chats To Display.....</p>
                        <p id="initMsg">!!...Click On User Or Group Button To Start Chat...!!</p>
                        <ul id="messages"></ul>
                    </div>
                    <div class="panel-footer input-group" style="width:100%">

                        <form id="chatForm" action="" onsubmit="return false">
                            <input id="myMsg" class="input-box-send  form-control" ng-model="message" autocomplete="off" style="width:80%" placeholder="Write Message Here.." />
                            <button type="submit" id="sendBtn" class=" btn btn-primary " ng-disabled="!message" name="btn"><span class="glyphicon glyphicon-send"></span></button>
                        </form>
                    </div>
                </div>
            </div>

        </div>
    </div>
</main>

Don't use function expression $scope.searchFn = function () {} . 不要使用函数表达式$scope.searchFn = function () {} Function expressions in JavaScript are not hoisted. JavaScript中的函数表达式未悬挂。 Use named functions to avoid such cases 使用命名函数来避免这种情况

function searchFn() {...}

You are not using an angular directive for triggering the keypress event. 您没有使用角度指令来触发按键事件。 The " onkeypress " is an html attribute so any function declared on $scope will not be accessible. onkeypress ”是一个html属性,因此在$ scope上声明的任何函数将无法访问。 You should use ng-keypress directive . 您应该使用ng-keypress指令

Before: 之前:

  <input id="input" class="form-control" name="browser" placeholder="Search friend.." ng-model="search" onkeypress="searchFn()">

After: 后:

<input id="input" class="form-control" name="browser" placeholder="Search friend.." ng-model="search" ng-keypress="searchFn()">

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

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