Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 仅在命中第一个键入的字母时显示seachresult_Angularjs - Fatal编程技术网

Angularjs 仅在命中第一个键入的字母时显示seachresult

Angularjs 仅在命中第一个键入的字母时显示seachresult,angularjs,Angularjs,我在玩AngularJs,下面的代码是一个搜索功能。问题是,在我输入之前,它总是显示所有的名字列表。在我的名单中,我将有200多个名字(这是一个很长的名单)。我试图做到的是,当我点击文本框中的第一个字母时,只显示列表。我怎样才能做到这一点谢谢 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script> <div ng

我在玩AngularJs,下面的代码是一个搜索功能。问题是,在我输入之前,它总是显示所有的名字列表。在我的名单中,我将有200多个名字(这是一个很长的名单)。我试图做到的是,当我点击文本框中的第一个字母时,只显示列表。我怎样才能做到这一点谢谢

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.min.js"></script>

<div ng-app="">
                <div ng-init="friends = [{name:'John'},
                       {name:'Mary'},
                       {name:'Mike'},
                       {name:'Adam'},
                       {name:'Julie'},
                       {name:'Juliette'}]"></div>
                <span style="color: white">Search:</span> <input ng-model="searchText">
                <table style="color: white" id="searchTextResults">
                    <tr><th>Name</th></tr>
                    <tr ng-repeat="friend in friends | filter:searchText">
                        <td>{{friend.name}}</td>
                    </tr>
                </table>

            </div>


<script>
            var expectFriendNames = function (expectedNames, key) {
                element.all(by.repeater(key + ' in friends').column(key + '.name')).then(function (arr) {
                    arr.forEach(function (wd, i) {
                        expect(wd.getText()).toMatch(expectedNames[i]);
                    });
                });
            };

            it('should search across all fields when filtering with a string', function () {
                var searchText = element(by.model('searchText'));
                searchText.clear();
                searchText.sendKeys('m');


                searchText.clear();
                searchText.sendKeys('2');

            });
            </script>

搜索:
名称
{{friend.name}
var expectFriendNames=函数(expectedNames,键){
元素.all(通过.repeater(key+'in friends')。列(key+'.name'))。然后(函数(arr){
arr.forEach(功能(wd,i){
expect(wd.getText()).toMatch(expectedNames[i]);
});
});
};
它('使用字符串过滤时应搜索所有字段',函数(){
var searchText=element(by.model('searchText');
searchText.clear();
searchText.sendKeys('m');
searchText.clear();
searchText.sendKeys('2');
});

仅当搜索文本不为空时才显示表格:

<table ng-show="searchText.length != 0">


谢谢您的回答。但这是这样的:当我没有在文本框中输入任何内容时,整个表格都会显示出来。首先,当我在texbox中键入一些内容,然后删除表中变为空的文本时,请确保在控制器中将searchText初始化为空字符串。您也可以使用
!!searchText
而不是
searchText.length!=0
我认为最好使用
ng if
而不是
ng show
,因为
ng show
仍然会创建数百个元素并将其隐藏,尽管
ng if
只是不创建任何元素。@allenhwkim这是内存使用和dom更新速度之间的折衷。但你是对的,ng if也可以使用。