将带有属性的自定义筛选器传递到角度中的自定义指令

Passing a custom filter with attributes to a custom directive in angular

本文关键字:自定义 指令 属性 筛选      更新时间:2023-09-26

有没有办法将自定义筛选器作为属性传递到自定义指令中?所以如果我有指令

<my-element value="1234" filter="my-filter:attr1:attr2"><my-element>

作为

angular.module("directives", ["filter"]).directive("myElement", function() {
    return {
    restrict: "E",
        replace : true,
        scope   : {
            value   : "@",
            filter  : "@"
        },
        template: function (el, attr) {
            return "<p>{{value | filter}}</p>";
        },
        link: function($scope){ /* linking stuff */ }
    };
});

带过滤器:

angular.module("filter", []).filter("myFilter", function() {
    return function(value, attr1, attr2) {
            return "filtered value";
        };
    };
});

只是什么都没发生,我不知道这里出了什么问题。。。

您是否将这两个模块注入到应用程序模块中?像这样:

angular.module("app", ["filter","directives"])