Beginning AngularJS

(WallPaper) #1
Chapter 4 ■ Filters and Modules

Using ngRepeat, we loop through the monthlyUsageHistory array and output each value (which itself is
formatted using the number filter). As Figure 4-5 shows, only the first five items are displayed. This is because we did
not, in fact, loop through the monthlyUsageHistory array. What we actually did was loop through a totally new array;
an array which was produced by the limitTo filter.


Figure 4-5. Using the limitTo filter to display a subset of data


Figure 4-6. Using limitTo to show items from the end of an array


Of course, if you wanted to show only the first three items, you could do so by using limitTo:3. What if you
wanted to show only the last three items? Specifying a negative value can do this. If you replace the li element in
Listing 4-7 so that it uses the following code snippet, you should see results like those shown in Figure 4-6.



  • ■ Tip Filters do not change the underlying data upon which they operate.


    There are other handy built-in filters that you can use, and you can find these in the API documentation at
    https://docs.angularjs.org/api/ng/filter. We will take a quick time-out from filters now and look at Angular
    modules. An understanding of modules will put us in a much better position to tackle custom filters; which we will do
    in the last section of this chapter.

  • Free download pdf