2016-02-10 160 views
0

我遇到的問題是搜索過濾器不會過濾表。有什麼問題的建議或幫助?我製作的應用程序很簡單,用戶需輸入兩個文本字符串並保存,以便數據可以存儲在Firebase數據庫中。然後通過搜索來篩選能力。但是,搜索不起作用。Angular JS搜索過濾器沒有使用Firebase過濾數據

Click here to access plunker

的index.html

<table class="table table-bordered"> 
    <thead> 
    <th>Plate Number</th> 
    <th>Car Brand</th> 
    </thead> 
    <tbody> 
    <tr ng-repeat="customer in customers | filter:search"> 
     <td>{{customer.CustomerPlateNumber}}</td> 
     <td>{{customer.CustomerCarBrand}}</td> 
    </tr> 
    </tbody> 
</table> 

的script.js(使用火力)

angular.module('myApp', []); 

angular.module('myApp').controller('customerCtrl', function($scope) { 

    $scope.CustomerPlateNumber = ""; 
    $scope.CustomerCarBrand = ""; 
    $scope.customers = {}; 

    $scope.myData = new Firebase("https://sizzling-torch-2102.firebaseio.com/CustomerInformation"); 

    // PS, husk at CustomerPlatenumber: må være lik navnet på ng-model. 
    $scope.saveCustomerData = function() { 
    $scope.myData.push({CustomerPlateNumber: $scope.CustomerPlateNumber, CustomerCarBrand: $scope.CustomerCarBrand}); 

    // Empty input after storing data 
    $scope.CustomerPlateNumber = ""; 
    $scope.CustomerCarBrand = ""; 

    }; 

    // Two parameters, needs to know what its going to be fired upon, and a function that tells what to do when it is fired. 
    $scope.myData.on('value', function(snapshot) { 
    $scope.customers = snapshot.val(); 
    //not recommended, look at "refresh bindings angular" 
    $scope.$apply(); 
    }); 

}); 

回答

1

的問題是,$ scope.customers不是一個數組[]而是一個複雜的JavaScript對象{}。

如果你把它改爲:

$scope.customers=[]; 

並轉換是火力地堡被返回Array的複雜的哈希表,它的工作原理(爲了說明的緣故,我會首先檢查火力地堡有方法返回一個數組而不是使用這個特殊的解釋代碼):

$scope.myData.on('value', function(snapshot) { 
var values = snapshot.val(); 
for(var myVal in values) 
{ 
    var item = values[myVal]; 

    $scope.customers.push(item); 
} 
}); 
+0

謝謝Daniel Lesser,這個工作很完美。我將檢查firebase是否有返回數組的方法。 –