通过注入$filter,Angular 模块中任何定义的过滤器都可以用于控制器、服务、指令甚至其他过滤器。
angular.module("app") .service("users", usersService) .controller("UsersController", UsersController); function usersService () { this.getAll= function () { return [{ id: 1, username: "john" }, { id: 2, username: "will" }, { id: 3, username: "jack" }]; }; } function UsersController ($filter, users) { var orderByFilter = $filter("orderBy"); this.users= orderByFilter(users.getAll(), "username"); // 现在用户按他们的用户名排序: jack, john, will this.users= orderByFilter(users.getAll(), "username", true); // 现在用户按他们的用户名排序,以相反的顺序:will, john, jack }