I have been working through an AngularJS book to learn, but I am struggling with some code that is not well explained.
The selectCategory() function is included in the ng-click directive like this:
<a ng-click="selectCategory()">Home</a>
<a ng-repeat="item in data.products | orderBy:'category' | unique:'category'"
ng-click="selectCategory(item)">
{{item}}
</a>
Here is the filter used to filter products based on categories when the ng-click directive is triggered:
ng-repeat="item in data.products | filter:categoryFilterFn">
These functions are defined as follows:
angular.module("sportsStore")
.controller("productListCtrl", function ($scope, $filter) {
var selectedCategory = null;
$scope.selectCategory = function (newCategory) {
selectedCategory = newCategory;
}
$scope.categoryFilterFn = function (product) {
return selectedCategory == null ||
product.category == selectedCategory;
}
});
I am having trouble understanding these codes, especially how the functions are defined. Why is $filter included in the argument? Can you please explain these definitions to me? I am new to this and would really appreciate your help. Thank you!