0

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
  • 2
    Hey, welcome to SO, you may want to share your code. Commented Dec 17, 2015 at 14:41

1 Answer 1

1

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

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

4 Comments

How can I use the same filter in another application?
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.
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?
Also, can you please answer this question? stackoverflow.com/questions/38792017/…

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.