This course will be retired on January 6, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Using everything we’ve learned in this course, in this video we’ll make some final UI enhancements to our application.
The array.filter() method in the Mozilla Docs.
You can find the final app here: https://w.trhou.se/6l799nn93n
-
0:00
Our application is looking good, but
-
0:02
there are a couple of things missing before it's complete.
-
0:06
First of all,
-
0:07
the save functionality is limited to the TODO where the Save button is clicked.
-
0:13
For instance, if I click Save here, it will only save this TODO.
-
0:19
A better behavior would be to save all TODOs that have been edited
-
0:23
whenever the save button is clicked.
-
0:26
So for instance, if I click this save button,
-
0:29
even if I have edited TODOs down here, all of them will be saved.
-
0:34
Second, the edit button is a bit of a UI flaw.
-
0:39
As a user whenever you are working with a modern web application you expect
-
0:44
to be able to edit the application without explicitly telling it that you want to.
-
0:49
So in our app I have to click edit if I want to edit a to do.
-
0:54
What should happen is that I'm able to click the to do and start editing.
-
0:59
So let's fix our save functionality first.
-
1:06
First, I'm going into our data service.
-
1:09
Then I'm going to change saveTodo to saveTodos.
-
1:13
Now, on the controller I'll also change the name of the function.
-
1:20
Just to be more semantically correct.
-
1:25
My new save function is not going to take any parameters.
-
1:30
Instead, I'm going to filter the scope.Todos for
-
1:35
all Todos that have been edited.
-
1:50
Remember that scope.todos is an array of objects.
-
1:54
This means that I can use the array's filter method.
-
1:58
And this will just filter the array into a new array.
-
2:05
The filter method takes a callback function.
-
2:07
And this function will take a parameter
-
2:10
that will be the name of the current element in the array inside of a callback.
-
2:15
We can name it anything, but in an iterater, typically the singular form
-
2:20
of the variable that you're iterating over is a good convention to go by.
-
2:25
And here we'll say if to-do has
-
2:30
been edited, And return it.
-
2:50
We can call this new set of todos filtered
-
2:53
Todos And
-
2:59
that's what we'll pass to our saveTodos function in our service.
-
3:04
We'll just check that this works.
-
3:07
Refresh the page.
-
3:09
Open my console.
-
3:12
We'll edit.
-
3:15
Couple of these Todos.
-
3:22
Now, we'll click save Todos.
-
3:25
Looks like it's not working.
-
3:32
That's because we forgot to update our template with our new function name
-
3:37
in the controller and that save Todos, and I'll refresh the page.
-
3:49
Edit a couple of TODOs.
-
3:52
Now I can see it's working.
-
4:04
But of course, I need to change the parameters in the service.
-
4:18
We'll change this to be a little more accurate.
-
4:29
Refresh the page again.
-
4:51
There we go.
-
4:51
We see the number of todos that have been changed.
-
4:58
Great.
-
4:59
One thing is missing.
-
5:00
If I check a TODO as completed, or
-
5:03
if I uncheck a TODO as incomplete, they don't get saved.
-
5:14
You can see that there's still the same number of todos being saved.
-
5:17
To solve this we're going to use the exact same expression
-
5:23
we used on the ng change directive on our text input.
-
5:29
Only now we're going to use this On the ng-click directive,
-
5:39
In the span element.
-
5:43
Since we already have one expression,
-
5:45
where we add another expression is with a semicolon.
-
5:50
We'll say todo.edited is true.
-
5:57
Refresh the page.
-
6:00
I have 1, 2, 3 to do's, save those, 3 to do's have been saved.
-
6:08
4, 5, save those, 5 to do's have been saved.
-
6:14
I want to point out that typically we could use the ng-change directive
-
6:18
on the check box input just like we did on the text input.
-
6:23
However, remember that we have a custom input directive, and since the CSS style
-
6:29
is set to display=none, Angular won't pick up the changes to the check box input.
-
6:34
Good thing it's just as easy for
-
6:36
us to use the ng-click directive on the span element.
-
6:40
Finally, let's make the user experience around editing a little more intuitive.
-
6:44
And we can do this by adding
-
6:49
the ng-click directive to the label element.
-
6:55
And we'll say ng-clikc="editing = true".
-
7:02
Save that.
-
7:04
Refresh the page.
-
7:07
Now when I click on the label, the input opens up.
-
7:14
So of course now we can get rid of the editing button.
-
7:21
And our UI is just that much more sleek and that much more intuitive.
-
7:27
Great job in this course.
-
7:29
You now have the confidence to dissect smaller angular projects with little or
-
7:33
no guidance.
-
7:34
You may want to go back over some of the content in this course before you feel
-
7:37
comfortable setting up your own Angular application.
-
7:41
So don't feel like you should have Angular mastered just yet.
-
7:45
Other Treehouse content will help you master Angular and
-
7:48
make you feel more comfortable with large applications.
-
7:51
For now, take pride in what you've accomplished in this course.
-
7:55
Great work!
You need to sign up for Treehouse in order to download course files.
Sign up