【问题标题】:Reset Filter In AngularJs在AngularJs中重置过滤器
【发布时间】:2020-02-06 14:06:55
【问题描述】:

我正在尝试通过单击一个按钮来重置我的过滤器。我尝试将值设置为空,但所做的只是完全删除选项列表。我希望能够将它们设置为空并在无需重置我的网络应用程序后再次重复使用它们。

我的 JS。

$scope.resetFilters = function() {

        $scope.itemCat = {};

    }

我的 HTML。

<div class="card-header">
            <div class="col-12 text-center">
                <h1>Current Postings</h1>
            </div>
            <button type="button" class="btn btn-success" ng-click="resetFilters()">Reset Filters</button>
            <div class="row">

                <div class="col-lg-4 text-center">
                    <label for="searchByName">Filter by Keyword:</label><input type="text" id="searchByName"
                        class="form-control" ng-model="filteritem.itemName">
                </div>

                <div class="col-lg-4 text-center">
                    <label for="searchByCat">Filter by Category:</label><select
                        class="form-control" id="searchByCat" ng-options="value for value in itemCat"
                        ng-model="filteritem.itemCat">
                    </select>
                </div>

                <div class="col-lg-4 text-center">
                    <label for="searchByLocation">Filter by Location:</label><input
                        type="text" id="searchByLocation" class="form-control" ng-model="filteritem.location">
                </div>
            </div>

        </div>

我的过滤器影响的 HTML。

<div class="card-body">
        <div class="row">

            <div class="col-md-3" ng-repeat="item in items | filter : filteritem">
                <div class="card">
                    <img src="data:image/png;base64,{{item.thumbnail}}"
                        class="card-img-top img-fluid">
                    <div class="card-block">
                        <h4 class="card-title">{{item.itemName}}</h4>
                        <p class="text-success">{{'$' + item.itemPrice}}</p>
                        <p class="card-text">
                            <small class="text-muted">{{item.location}}</small> <br>
                        </p>

                    </div>
                    <button type="button" ng-click="selectItem(item)"
                        class="btn btn-danger">View Item</button>
                </div>
                <br>
            </div>
        </div>
    </div>

【问题讨论】:

    标签: html angularjs filter


    【解决方案1】:

    代替$scope.itemCat = {}; 试试$scope.filteritem.itemCat=null;

    【讨论】:

      【解决方案2】:

      您将类别列表设置为空,而不是过滤字段。

      删除这个:

      $scope.itemCat = {};

      并将其替换为:

      $scope.filteritem.itemName = null;
      $scope.filteritem.itemCat = null;
      $scope.filteritem.location = null;
      

      【讨论】:

      • 由于某种原因这不起作用。我收到此错误 angular.js:14800 TypeError: Cannot set property 'itemName' of undefined。我在帖子中添加了更多 HTML。我试过 $scope.filter.itemName = null;一次之前认为它会起作用。但无济于事。
      • 你能在你的控制器中定义$scope.filteritem 的地方发布吗?错误提示 itemName 不存在
      • 我没有在任何地方的 JS 中指定它。但是,我的过滤器确实有效。
      • 在这种情况下,如果您在 resetFilters 方法中设置 $scope.filteritem = null 会发生什么?
      • itemName 来自我在 angularJS 中完成的 http 请求。我正在向我的服务器发出请求,然后从我的数据库中获取数据并用(项目)响应我的前端。我正在使用 ng-repeat="item in items | filter : filteritem" 在我的视图中填充这些项目。要根据 itemName、itemCat 或位置过滤项目,我在我的 html 中使用 3 个输入来执行此操作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多