4

I've an angular project, structured using by-module-type principle:

├── directives
├── services
├── filters
│   └── filters.js
└── states
    └── home
        ├── home.html
        └── home.module.js

I've created a custom filter in filters.js

import angular from "angular";

export default angular.module("app.filters", [])
    .filter("removeStartingWithDollar", function(items) {
        var filteredItems = [];
        angular.forEach(items, function(item) {
            if (!item.startsWith('$')) { filteredItems.push(item);  }
        });
        return filteredItems;
    });

I want to use this removeStartingWithDollar filter in my home.html template. How do I inject it as a dependency and should I do that at all?

2 Answers 2

4

You have to add that module in the module you have to use the filter, for example, imagine you have an app module:

angular.module('app', ['app.filters'])

After that you will use it

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

Comments

2

Just inject the app.filters module in anywhere you need to use any provider of it.

angular.module("MySecondModule", [app.filters])

3 Comments

I got a down vote for my answer. I'm sure he can't explain it ;)
The down vote may be because you are injecting a variable rather than a string. The other answer is using a string. I wouldn't go as far as to say its wrong, because it depends on what variables you have floating around in the rest of your code, but it certainly would introduce a dependency on that variable existing at creation time, that the string version doesn't have.
Design time dependency. You have a point there. I will not edit my answer - so, someone else can learn the difference between two answers. The above down vote should have been down vote + a comment. Now we have some!

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.