javascript - 在 Angular JS 中搜索列表

标签 javascript angularjs

我在一个页面上有一个搜索框,即 header.html 文件,而我要实现搜索功能的列表在另一页上,即 content.html .那么在这种情况下我如何使用 angularjs 搜索功能。以下是 html 代码。

header.html

<div class="input-group col-md-12 search-inner-page ">
   <input type="text" class="form-control" placeholder="Search" name="q">
</div>

content.html

<div class="surveyList" ng-repeat="survey in allSurveys">
    <span class="checkbox" ></span>
    <div class="toogleSurvey row" ng-mouseover="hoverIn()" ng-mouseleave="hoverOut()">
        <div class="col-xs-5 col-sm-2  col-md-2">{{survey.Name}}</div>
        <div class="col-xs-5 col-sm-1 col-md-1">{{survey.Type}}</div>
        <div class="col-sm-3  col-md-3 hidden-xs">{{survey.SurveyReference}}</div>
        <div class="col-sm-3  col-md-3 hidden-xs">{{survey.CreatedDate}}</div>
        <div class="col-sm-2  col-md-2 hidden-xs SurveyLastChild">{{survey.Status}}</div>
        <div class="hidden-xs surveyListTool"  ng-show="hoverEdit">
        <a class="editSurvey" title="edit"></a>
        <a class="deleteSurvey" title="delete"></a>
        <a class="exportSurvey" title="export survey"></a>
        <a class="menuSurvey" title="menu"></a>
    </div>
</div>

contentCtrl.js

angular.module("adminsuite").controller("surveyController",['getAllSurveyService','AuthenticationService', '$scope','$rootScope', '$state', function(getAllSurveyService,AuthenticationService, $scope,$rootScope,$state){

getAllSurveyService.surveyList().then(
function( data ) {
    $scope.allSurveys = data;
    console.log($scope.allSurveys);
    if($scope.allSurveys.ErrorMessage){
      AuthenticationService.ClearCredentials();
      $state.go('login');
    }
}
 );

 }]);

headerController.js

angular.module("adminsuite").controller("headerController",['AuthenticationService','$rootScope','$cookieStore','$scope',function(AuthenticationService,$cookieStore,$scope,$rootScope){
   $scope.header = "Header";
  $scope.searchInSurvey = $scope.surveySearch;
     $scope.logout = function(){
                //$scope.dataLoading = true;
                AuthenticationService.ClearCredentials();
                console.log($cookieStore.get('globals'));
                //$state.go('login');
            };
  }]);

在上面提到的 header.html 的搜索框中输入内容时,它应该搜索 content.html 页面中的内容。

最佳答案

您可以在 header.html 的输入上使用 ng-change 来更新 allSurveys 数组。

只需在您的输入中添加一个ng-change 和一个ng-model

<div class="input-group col-md-12 search-inner-page ">
  <input ng-model="yourCtrl.searchInput" ng-change="yourCtrl.searchInputChanged" type="text" class="form-control" placeholder="Search" name="q">
</div>

在您的 Controller 中,您可以添加一个函数 searchInputChanged,它将使用 .filter() 根据 searchInput 过滤数据。

关于javascript - 在 Angular JS 中搜索列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39099292/

相关文章:

javascript - Angular ui 路由器 : accessing $stateParams on reload

javascript - 如何将 bootstrap popover 添加到 Angular ng-repeat

javascript - 如何使用 socketio 使用 Sails 实时显示在线/离线用户?

javascript按值在数组中查找对象并追加附加值

javascript - 我的 Angular 应用程序无法运行

javascript - 从动态数据生成 AngularJS UI 模板

javascript - 使用 AJAX 请求从另一个域获取图像数据

javascript - ng 嵌入为 : element vs attribute

JavaScript Excel.Application ActiveX cell.Value 返回 "date"类型的值,其原型(prototype)中没有 toString()

angularjs - 在 View 中看不到我的 html 文件