1

I am creating a custom Angular filter and I would like to provide the closest thing possbile to "named arguments" (a la Python), primarily for the purpose of boolean options so as to avoid passing an unreadable sequence of true/false values.

If I only had to consider calling from "raw" javascript I would accept some kind of options object:

myFilter(someValue, {
    foo: false,
    bar: true
});

However I would like this to be easy to use from within Angular expressions too. Is it possible to pass objects like this within an expression? If not, or perhaps if it is simply too awkard, what would be a more idiomatic approach?

1 Answer 1

2

Yeah why not, you can use named arguments easily in a filter:

app.filter("myFilter", function() {
    return function(item, filterConfig) {

        if (filterConfig.foo) {
            // return something
        } else {
            // return something else
        }
    }
});

And usage:

{{ myValue | myFilter:{foo:false} }}

See this jsfiddle

Sign up to request clarification or add additional context in comments.

4 Comments

I was looking for a way to use the filter from within an Angular expression; I have updated my question to make this clearer.
What do you mean with 'an angular expression'?
The things you put inside double curly braces; see docs.angularjs.org/guide/expression. Is there a better term for this?
Yeah you can use this also in there, see edited answer and added jsfiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.