带有angularjs的FilterText

时间:2014-02-19 07:41:32

标签: javascript angularjs angularjs-ng-repeat angularjs-filter

我有一个简单的searchController:

testapp.controller("searchController", function($scope, $rootScope, $http, $location) {

    $scope.filterText = null;

    var load = function() {
        console.log('call load()...');

        var url = 'product.json';

        if($rootScope && $rootScope.appUrl) {
            url = $rootScope.appUrl + '/' + url;
        }
        console.log(url);

        $http.get(url)
        .success(function(data, status, headers, config) {
            $scope.product = data;
            angular.copy($scope.product, $scope.copy);
        });
    }

    load();

}); 

如您所见,我已经声明了filterText。我的观点看起来像这样:

<div class="container main-frame" ng-app="testapp"
    ng-controller="searchController" ng-init="init()">
    <h1 class="page-header">Products</h1>
    <!-- Filter Start -->
    <div class="search-box row-fluid form-inline">
        <label>Filter: </label> <input type="text" ng-model="filterText" /> 
    </div>
    <div class="results-top"></div>
    <!-- Filter End -->
    <table class="table">
        <thead>
            <tr>
                <th width="25px">ID</th>
                <th>TITLE</th>
                <th>PRICE</th>
                <th>Description</th>
                <th width="50px"></th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="p in product track by p.id | filter: filterText">
                <td>{{p.id}}</td>
                <td>{{p.title}}</td>
                <td>{{p.price}}</td>
                <td>{{p.description}}</td>
            </tr>
        </tbody>
    </table>
    <!-- ng-show="user.username" -->
    <p>
</div>

我的问题是在输入内容时没有任何反应。有任何建议如何解决?

感谢您的回答!

1 个答案:

答案 0 :(得分:1)

track by表达相关的问题。

请尝试在track by

的末尾添加ng-repeat
<tr ng-repeat="p in product | filter: filterText track by p.id">

或者完全删除track by

我使用正确的代码创建JSFiddle。请参阅:

JSFiddle