Boris Burkov
Boris Burkov

Reputation: 14446

AngularJS: can I use filter, defined in another module?

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?

Upvotes: 4

Views: 2567

Answers (2)

Charlie
Charlie

Reputation: 23798

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

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

Upvotes: 2

Serginho
Serginho

Reputation: 7490

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

Upvotes: 4

Related Questions