Using AngularJS, how can I use a Filter (as out of the box feature) to support "String Processing" on "View Level"? knowing that implied JS function (that will implement such string processing) is under a common module & reusable from the whole AngulaJS application (i.e. can be recalled from any view under any module).
1 Answer
The short answer is Injection based on what version of angular you are using. You should show your code or create a public repo for us to help you. Here is a shot in the dark!... You want to create your filters at the app level and inject them into whatever controller you want to use them in.
example:
app.filter('upper', function () {
return function (input) {
return input.toUpperCase();
};
});
Then in view1.js do this:
.controller('view1', ['$scope','upper', function($scope, upper) {
var message = 'this is just an example';
}
and in your view do something like this:
<h1>{{message | upper}}
That should display : THIS IS JUST AN EXAMPLE
4 Comments
Vishal
How can I use the same filter in another application?
Sari Rahal
U'll need to use injection into the controller. If you look above, you can see that I create a filter, then I inject it into view1 controller. Then any view that uses the view1 controller I can use that filter.
Vishal
No, I am not asking that. suppose I have two modules called moduleA and moduleB then how can I use same filter in both modules? Here when you define a filter you are doing app.filter, so the module named app will be able to use it. But say if I have another module named app2 that wants to use this filter. Is it still possible to use the same filter in app2?
Vishal
Also, can you please answer this question? stackoverflow.com/questions/38792017/…