2016-08-23 88 views
5

名單我有一個頁面一個搜索框,即了header.html文件和列表上,我想要實現的搜索功能上的另一頁即content.html。那麼如何在這種情況下使用angularjs搜索功能。以下是html代碼。搜索在角JS

了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頁。

+0

你還有什麼在header.html中?請使用輸入文本元素來做其他事情嗎? – Aravind

回答

0

感謝你們。我得到了這樣的答案...

了header.html

<div class="input-group col-md-12 search-inner-page"> 
         <input type="text" class="form-control" placeholder="Search" name="q" ng-model="global.search"> 
         <img src = "images/search.png" alt = "Generic placeholder thumbnail" > 
        </div> 

內容。HTML

<div class="surveyList" ng-repeat="survey in allSurveys | filter:global.search"> 
<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> 

headerCtrl.js

$rootScope.global = { 
    search: '' 
}; 
0

您可以在header.html的輸入中使用ng-change來更新allSurveys數組。

只需添加一個ng-changeng-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> 

在你的控制器,你可以添加一個功能searchInputChanged將使用.filter()篩選基於searchInput數據。

+0

如果我爲這兩個視圖使用兩個不同的控制器,那麼它會在這種情況下工作嗎? – Kishan

+0

如果你使用2個控制器,你應該創建一個服務。您的服務可以返回content.html中使用的數組「allSurveys」,而header.html中的用戶控制器可以調用相同的服務來設置過濾器。 – Eric

+0

你能告訴我如何在我的例子中使用服務?我已經從「調查控制器」中獲得了所有調查。如何爲其創建過濾器 – Kishan

0

您可以設置一個變量爲$ rootScope您調查的名單,並從另一個控制器得到它的價值,像這樣:

//Controller that contains the list 
$rootScope.surveysList = [s1,s2,s3,...,sn]; 

之後,你就可以得到這個變量,並設置到控制器您輸入的範圍。

$scope.allSurveys = $rootScope.surveysList; 

你輸入應該是這樣的:

<input ng-model="surveySearch" type="text" class="form-control" placeholder="Search" name="q"> 

而在你的列表模板闕查詢應該是這樣的:

ng-repeat="survey in allSurveys | filter:surveySearch" 

我希望它爲你工作

+0

這不適用於我 – Kishan

+0

您是否將$ rootScope設置爲兩個控制器的依賴關係? –

+0

我已經添加了你提到的所有東西。事情是ng-model =「surveySearch」與其關聯的特定控制器的範圍對象變量。所以如果我試圖訪問這個HTML模板以外如何訪問做到這一點? – Kishan