[MUSIC]
In part one of this lecture we went over the process of how to create custom
filters.
In this lecture we're going to go ahead and implement some of the custom filters
in the application that we've been dealing with so far.
So as you can see here, one of the messages we're returning
from our controller is Yaakov likes to eat healthy snacks at night.
So let's bump it up a bit.
Let's not say likes, let's say love.
But, in this case,
we're going to replace the word likes with loves using a custom filter.
So, let's go to our code editor and
the first step is to create our filter factory function.
So let's go ahead and see where the controller ends so
we can put our function right underneath that.
And we'll call this function LovesFilter.
And since this is a factory function, it needs to return another function.
And that function is going to take an input, and
we need to put a semicolon here.
And basically,
the first thing we're going to do is make sure that our input actually exists.
So we'll say, if it's input or if it's not there, we'll just give it an empty string.
And then we'll just replace the input by saying input.replace.
And we'll replace the word likes with the word loves.
Okay, and then obviously, we need to return that newly created input.
So, so far, so good.
Now we need to register our factory filter with the Angular application.
So let's go back up and instead of terminating the controller,
we'll do one more statement.
We'll say filter, and we want to call it loves, and
give it our factory function, which is LovesFilter.
Okay, so now we need to use this LovesFilter.
Since we already have a function called sayMessage, we'll go ahead and copy that,
and we'll create another one called sayLoves, or maybe sayLovesMessage.
Okay, and in this case, what we're going to do is,
we're going to apply our filter.
But before we can do that, we need to inject it into our controller.
So we'll go ahead and append one more array item into the dollar sign injector
array that's attached to our controller function, and we'll call it LovesFilter.
And remember, this loves comes from the filter name, and the filter is going
to be appended by Angular.js for us, so we have to name our filter LovesFilter.
And we also need to give it as a second argument,
since we placed it as a second item in the array of the injections, okay?
So now it's lovesFilter here too.
And now we could use this lovesFilter inside of our sayLovesMessage.
So what we're going to do is we're going to take this message and
we're going to call lovesFilter, pass it this message.
And actually we'll just override the message using that and
then we'll return the message.
So, so far so good.
If we go to the index HTML,
we'll see our original message which is right here, let's call it original.
And we'll put a break here, it's a little ugly but it will do.
The loves message will be similar, but this one will say sayLovesMessage and
we'll give it a paren, so we'll execute that function.
So when we save it, and since my browser sync is already working,
I'll go to my browser.
As you can see, love says Yakov loves to eat healthy snacks at night.