2017-06-10 20 views
0

在我angular應用程序,我必須在過濾器功能,但我無法得到我的輸出。我已將id存儲在數據庫中,但在ng-repeat中,我正在使用函數並從另一個作用域中獲取相關的基於id的名稱。它工作正常,但過濾器無法正常工作。如何在angularjs中使用過濾器與ID相關的名稱爲基礎的過濾器

注:IFilter的工作基於ID,我需要過濾器的基礎name,請幫忙如何做到這一點

模板代碼:

<input type="text" ng-model="json1.Name" class="searchbox" /> 
<hr/>Ng-Repeat<hr/> 
<div ng-repeat="json in myJson | filter:json1"> 
    <li> {{showCartName(json) || 'select'}}</li>  
</div> 

控制器:

angular.module('app',['QuickList']).controller('mainCtrl', function($scope,$filter){ 
    $scope.myJson = [{id:1},{id:2}] 
    $scope.myJson1 = [{id:1,Name:"one"},{id:2,Name:"two"}] 

    $scope.showCartName = function (json) { 
    var SelectStream = []; 
    if (json.id) { 

     SelectStream = $filter('filter')($scope.myJson1, { id: json.id }); 
    } 
    return SelectStream.length ? SelectStream[0].Name : 'Select'; 
    }; 
}) 

在這裏,我附上我的fiddle

+0

共享鏈接,採用以下結構'[鏈接](鏈接URL)' – Rajesh

+0

@Pengyy如何解決這個問題 – jose

+0

下一次當你問一個問題時,請考慮讓它更具可讀性。 :-) – Pengyy

回答

1

對於您的要求this answer就足夠了。

雖然您也可以使用custom filter,但這可以讓您靈活地實現您所需的任何邏輯。

在控制器中添加此:

$scope.customFilter = function (json){ 
     if ($scope.json1 === undefined) return true; 
     else{ 
     var Name = $scope.showCartName(json); 
      return (Name.indexOf($scope.json1) !== -1); 
     } 
    }; 

,並在HTML變化:

<div ng-repeat="json in myJson | filter:customFilter"> 

http://jsfiddle.net/HgDA7/638/

+0

嗨@shivam它的工作正常,但基於大寫可能是數據在大寫意味着我應該輸入大寫,然後只有工作幫助如何解決一個 – jose

+0

http://jsfiddle.net/HgDA7/640/你可以創建一個具有較低或較高外殼的臨時字符串,並將你的輸入轉換爲同一個外殼,並進行一次如Name.toLowerCase()的檢查。indexOf($ scope.json1 .toLowerCase())!== -1 OR Name.toUpperCase()。indexOf($ scope.json1.toUpperCase())!== -1 –

+0

謝謝一次檢查https://stackoverflow.com/questions/44568522/enter -value-auto-caps-in-editable-text-using-angularjs @Shivam Tiwari – jose

0

我認爲這足以滿足您的要求。

<div ng-app='app' ng-controller='mainCtrl'> 
<input type="text" ng-model="json1.Name" class="searchbox" /> 
<hr/>Ng-Repeat<hr/>`enter code here` 
<div ng-repeat="json in myJson1 | filter:json1.Name"> 
    <li> {{showCartName(json) || 'select'}}</li> 
</div> 

http://jsfiddle.net/HgDA7/636/

+0

我的數據看起來像這樣在我的小提琴中,我需要這種類型的過濾器:)@ thanveer pu – jose