1

我使用这样声明的指令:

    (function (directives) {
    var FilterDirective = (function () {
        function FilterDirective() {
            var directive = {};
            directive.restrict = 'A';
            directive.scope = true;
            directive.controller = elasticui.controllers.FilterController;
            directive.link = function (scope, element, attrs, filterCtrl) {
                scope.$watch(element.attr('eui-filter') + " | euiCached", function (val) { return scope.filter.filter = val; });
                var enabled = false;
                var enabledAttr = element.attr('eui-enabled');
                if (enabledAttr) {
                    scope.$watch(enabledAttr, function (val) { return scope.filter.enabled = val; });
                    enabled = scope.$eval(enabledAttr);
                }
                scope.filter = {
                    filter: scope.$eval(element.attr('eui-filter') + " | euiCached"),
                    enabled: enabled
                };
                filterCtrl.init();
            };
            return directive;
        }
        return FilterDirective;
    })();
    directives.FilterDirective = FilterDirective;
    directives.directives.directive('euiFilter', FilterDirective);
})

该指令的控制器是:

    (function (controllers) {
    var FilterController = (function () {
        function FilterController($scope) {
            this.scope = $scope;
        }
        FilterController.prototype.init = function () {
            var _this = this;
            if (this.scope.filter.filter) {
                var isEnabled = this.scope.filters.contains(this.scope.filter.filter);
                if (!isEnabled && this.scope.filter.enabled) {
                    this.scope.filters.add(this.scope.filter.filter);
                    isEnabled = true;
                }
            }
            this.scope.filter.enabled = isEnabled;
            this.scope.$watch('filter.enabled', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    _this.updateFilter();
                }
            });
            this.scope.$watch('filter.filter', function (newVal, oldVal) {
                if (!elasticui.util.EjsTool.equals(oldVal, newVal)) {
                    if (oldVal) {
                        _this.scope.filters.remove(oldVal);
                    }
                    _this.updateFilter();
                }
            });
        };
        FilterController.prototype.updateFilter = function () {
            if (!this.scope.filter.filter) {
                return;
            }
            if (this.scope.filter.enabled) {
                this.scope.filters.add(this.scope.filter.filter);
            }
            else {
                this.scope.filters.remove(this.scope.filter.filter);
            }
        };
        FilterController.$inject = ['$scope'];
        return FilterController;
    })();
    controllers.FilterController = FilterController;
})

实际上,该指令的作用域包含一个filter包含两个属性的对象,filter : { enabled : boolean, filter : object}并且该指令的使用方式如下:

        <label class="btn"  ng-model="filter.enabled"
            eui-filter="ejs.TermFilter('field','value')"  btn-checkbox>

单击按钮时,已设置 filter.enabled。我的目的是添加一个允许通过指令外部的函数更改 filter.enabled 值的行为。

该指令将如下所示:

        <label class="btn"  ng-model="filter.enabled"
            eui-filter="ejs.TermFilter('field','value')" eui-enable-fn="fn(somevariable)" btn-checkbox>

将在哪里fn获取并将somevariable其设置为filter.enabled.

提前致谢,

4

1 回答 1

1

如果您想通过按钮的压力启用/禁用过滤器,为什么不声明一个将属性eui-enabled设置为自定义切换变量的过滤器呢?

换句话说,它会导致:

HTML:

<label class="btn" eui-filter="..." eui-enabled="my_toggling_variable">

<button type=button ng-click="toggleVar()"></button>

JS:

myApp.controller('myCtrl', ['$scope', function($scope) {

  $scope.my_toggling_variable = false;

  $scope. toggleVar = function(){
    $scope.my_toggling_variable = !$scope.my_toggling_variable;
  };

}]);

希望能很好地理解这个话题。

于 2016-02-11T11:03:21.090 回答