0

I have the following factory:

app.factory("ModuleFactory", function (api, $http, $q,filterFilter) {
var moduleList = [];
var categoryList = [];
var moduleTypeList = [];
var academyModuleTypeList = [];
var mostUsed = [];
var lastUpdated = null;
return {
    /**
     * @description This function gets the entire module list for the given users organization.
     * @author Marc Rasmussen
     * @returns {d.promise}
     */
    getList: function () {
        var d = $q.defer();
        if (moduleList.length == 0) {
            $http.get(api.getUrl('module', null))
                .success(function (response) {
                    moduleList = response;
                    lastUpdated = new Date();
                    d.resolve(response);
                });
        }
        else {
            d.resolve(moduleList);
        }
        return d.promise;
    },
    getMostUsed: function () {
        var d = $q.defer();
        if(moduleList.length <= 0){
            this.getList().then(function () {

            })
        }
    }
}
});

Now the list moduleList contains a list of objects in these objects there is a fieldnum_used

As you can see i have a created a function called getMostUsed this function needs to return the moduleList but ordered by the field num_used desc.

However i am not quite sure how to use filterFilter for this, i know that i could just use array.sort() however i wish to use the angular function for it if it is possible?

Can anyone point me in the right direction?

1
  • what is filterFilter? The name of a filter you defined? Commented Oct 6, 2015 at 17:10

1 Answer 1

2

Assuming filterFilter is a filter you defined somewhere in your app, you can invoke it in your controllers/factories like this.

Import the angular $filter service.

app.factory("ModuleFactory", function(api, $http, $q, $filter) {

And when you need it you can request your filter and call it like this:

 $filter("filterFilter")(moduleList);

More info is available on the angular documentation page for $filter service here.

If what you want to do is order your moduleList by a given property you should be using the orderBy filter.

this.getList().then(function(moduleList) {
  $filter('orderBy')(moduleList, '-num_used'); // order descending by the num_used property
})

More info about the orderBy filter can be found here.

Sign up to request clarification or add additional context in comments.

3 Comments

@tovsky can you give an example if we only used the build in $filter method of angular?
do you mean the default filter called filter? @MarcRasmussen
@MarcRasmussen I added an example using orderBy to order the module list in a descending order. I hope that's what you wanted. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.