The reason we had to set up a watch in postlink is because our $OnChanges,
our changes object, did not include any changes to the array.
Even though we put more things into the array.
Because OnChanges does not watch when elements are edited to the array.
It's only watching the actual reference to the array itself.
Now, as I was coding that example, I, myself,
did not realize they were using a little bit of an older version of Angular 1.
I was using Angular version 1.5.7.
Now there's something that happened in 1.5.8.
And that is that they put in one more lifecycle method
as part of the component lifecycle methods.
So right now, I'm located in the documentation for Angular 1.
And it's docs.angularjs.org/guide/component.
And just to make it more fun for us.
If we click the 1.5.8 and scroll down to those life cycle methods.
Let's take a look.
You'll see that there is the onInit,
the unchanges undestroy and post link.
But were missing another method that actually works in 1.5.8.
But its not listed in any documentation.
So let's go back to the default one.
Unfortunately, the snapshot documentation.
And we'll scroll down to those life cycle methods.
And we'll take a look.
Because there's yet another method we have that is called dollar sign do check.
And that's a method that gets called on each turn of the digest cycle.
So, as a digest cycle executes, it will call this method with no arguments.
And it tells you this hook is invoked with no arguments.
And if detecting changes, you must store the previous values for
comparison to the current values.
So basically, you have to check them yourself.
Okay, so that's what we're going to do.
We're going to drop try to set up our own watch.
Which just hooks in into the digest cycle.
And instead, we'll use the standard life cycle method, called doCheck.
So, let's go back to our code editor.
And, if you see here, I created a new lecture.
And right now, I'm locating lecture 33-1.5.8.
And again,
even though the documentation says this method doesn't exist, I tested it.
And it actually works, so it certainly does exist.
And this is exactly the same application we saw before.
The only difference is I edited couple of things and
removed couple of things, as well.
Number one is, I edited this local variable to
the ShoppinglistComponentController called totalItems.
And that local variable toalItems is going to get initialized
In the dollar sign on Init.
So the total items I'm speaking of is the total items in our shopping list.
So, on Init, it's going to initialize to zero.
Now, we'll leave this dollar sign on changes.
Even though it's not really going to be useful for
us, just to see the console.log of it that it's happening.
And what we did here, if you take a look, is we removed the dollar sign post link.
We actually removed the dollar sign on destroy as well.
Because we don't need it right now.
And, instead of setting up a watch,
we know that this gets called every time the digest cycle runs.
And every time the digest loop is looping.
So that means what we could do is we could ask, hey.
If the control that items that link that array.
Because our bindings watch only is looking at the items referenced itself.
Its not looking at the items of the actual array.
So, if we could take a look and see is the length of the array the same, or
not the same, as the number of total items.
If it's not the same, we need to remember the number of
total items that is contained in the array right now.
And since it changed.
Obviously, because it's not the same, we're going to go ahead and
invoke this cookiesInList method.
And if cookies is in list, that's going to return true.
We're going to have those same jQuery methods.
And we're going to call in order to basically slide down an error message.
And if total number of items is equal to the length of the array.
Which means nothings changed on the list.
We're going to go ahead and make sure that the error message slides up and goes away.
Okay. And just to for our own sanity,
let's put a couple of console log statements right here.
So, the first one we're logging.
We're going to log inside of this if statement.
Which means that the total number of items is not equal to the link of the array.
It means that the number of items change in our shopping list.
Either it went up or went down.
So, let's log and say something like,
number of items changed, checking for cookies.