无法从自定义AngularJS过滤器更新控制器的范围级别属性

时间:2015-03-24 07:06:16

标签: javascript angularjs

我想在UI中显示表中存在的记录数。但我无法更新过滤器内控制器范围内的记录属性。请帮助我。

<p>{{records}}</p>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Customer Account</th>
            <th>Storage</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="customer in (customerList | filterGridData:records)">
            <td>{{customer.display_name}}</td>
            <td>{{customer.storageStatus || "loading..."}}</td>
            <td>{{customer.state}}</td>
        </tr>
    </tbody>
</table>

控制器 -

$http.get(list.json).then(function(response){
   $scope.customerList = data;
   $scope.records = $scope.customerList.length; //use for displaying no of records in the table 
});

过滤 -

 filterGridData(records) {
    // some filter logic on the customerList data and then the filtered data will 
    // be displayed in the table and also the updated no of records should be displayed
    records = filterData.length;
}

1 个答案:

答案 0 :(得分:1)

您将无法更改过滤器中传递的records值,因为这是原始值,而原始类型是按值传递的。相反,你应该做这样的事情:

<p>{{filtered.length}}</p>
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Customer Account</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="customer in filtered = (customerList | filterGridData)">
            <td>{{customer.display_name}}</td>>
        </tr>
    </tbody>
</table>

您不再需要在控制器和过滤器中使用$scope.records