Tip:
Highlight text to annotate it
X
If you've been using Angular for a while, you probably
watched that last video and said, John you're an idiot.
You shouldn't have used a method here.
You should've used a filter.
So first, you really shouldn't call people idiots.
That's really mean.
Second, you're absolutely right.
We should've used a filter.
This is the perfect use case for a filter.
So let's take this reverse message and
turn it in to a filter.
Basically, all we have to do is learn the syntax of adding
a filter to your module, or myApp here.
And we'll just call it, reverse.
And the first function is the function that's going to
handle whatever we want to inject into it.
We're not going to inject anything, but I'll show you in
a second how that would work.
And this is going to return another function, which is
going to handle the input of the filter.
So the input's going to be the text.
And this is going to return the result of our filter.
So let's rename this message to text, because that's going
to be our input, and then all we have to
do is set this filter.
Let's just make sure this is working real quick.
So no filter applied.
And then the syntax for filter is just pipe, and then the
name of the filter, so it is reverse.
So let's see, filter applied.
So you can see this is working.
And now this is a much more reusable module-wide thing we
can use in any of our markup.
Or any [INAUDIBLE].
You can use that filter anywhere we want to, inside of
our module of course.
So just to show you the injection piece, we can inject
something from our module here.
So it'll say data, and we just do plus date of message.
And you'll see that this--
you can see it does our filter, the input of this,
then plus the date of message, which is really
just that same input.
But now it's coming from an injected
object, an injected model.
So that's how you set up filters.
It's pretty simple.
Just a name, that method's going to handle injection, and
then it's going to return a method that handles that
input, and then that'll return the result of whatever your
filter wants to do.