具有重置功能的角度指令

时间:2015-07-09 14:10:27

标签: javascript angularjs underscore.js

我有一个名为filterButton的指令,它允许用户选择过滤器选项并在右侧添加复选标记。现在我在filter.html中有一个重置按钮。用户单击重置按钮后,所有过滤器字段将重新设置为默认值。我尝试了几种方法,但没有用。有什么建议吗?提前谢谢。

app.js

app.directive('filterButton', function () {
return {
        restrict: 'EA',
        templateUrl: 'template/filter-button.html',
        replace: true,
        scope: {
                list: '=', //two-way binding
                selected: '=', //two-way binding
                field: '@', //one-way binding
        },
        link: function ($scope) {
            $scope.selected = [];
            $scope.setSelectedOption = function() {
                var id = this.option.id;

                if(_.contains($scope.selected, id)) {
                    $scope.selected = _.without($scope.selected, id)
                } else {
                    $scope.selected.push(id);
                }
                return false;
            };
            $scope.isChecked = function(id) {
                if(_.contains($scope.selected, id)) {
                    return "fa fa-check pull-right-selected";
                }
                return "";
            };
        }
    }
});

过滤button.html

<div class="row filter-item">
<div class="btn-group" dropdown is-open="{open: open}">
    <button type="button" class="btn btn-primary dropdown-toggle" dropdown-toggle ng-disabled="disabled">Fiter By {{field}} <span class="caret"></span></button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenu">
        <li data-ng-repeat="option in list" class="dropdown-btn"> <a data-ng-click="setSelectedOption()">{{option.name}}<span data-ng-class="isChecked(option.id)"></span></a></li>
    </ul>
</div>

filter.html

<filter-button list="statusList" selected="selectedStatus" field="Status" popover="Error Customer? or No Error Customer?" popover-trigger="mouseenter"></filter-button>
<filter-button list="systemList" selected="selectedSystemType" field="System Type" popover="N+1 or Stand alone?" popover-trigger="mouseenter"></filter-button>
<filter-button list="customerFilterList" selected="selectedCustomer" field="Customer" popover="Male or Female?" popover-trigger="mouseenter"></filter-button>
<div class="row filter-item">
    <div class="filter-label">
        <button class="btn btn-primary">Reset</button>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

您可以在重置点击按钮上重新初始化所选数组:

<button class="btn btn-primary" ng-click="reset()">Reset</button>

在你的控制器中:

$scope.reset = function() {
    $scope.selectedStatus = [];
    $scope.selectedSystemType = [];
    $scope.selectedCustomer = [];
}