1

i have a function

function propValueFilter (toFilterArr, filterFunction) {
    return toFilterArr.filter(filterFunction);
};

and a factory

backpackrApp.factory('itemsFactory', function(propValueFilter) {

    factory.getBackpackById = function(id) {
    return backpacks.propValueFilter(function(element) {
        return element.id == id;
    });
};
return factory;
});

I am getting a Unknown Provider error in component $injector in the Webconsole.

How to inject a function right? I want to create a set of Helper functions which i can use in many controllers / factorys etc.

Thanks in Advance

3
  • I can't tell what you're asking. Angular will not inject the propValueFilter function, you need to create it as a factory I think, or provide a factory or service that returns an object with your utility functions as properties. Have you tried the egghead.io tutorials and read pages like this on dependency injection? Commented Dec 2, 2013 at 20:28
  • yeah, i've tried the egghead.io tutorials. but the second link looks good. I am very new to angular, thanks for input Commented Dec 2, 2013 at 20:31
  • that helped me to solve the problem, thanks Commented Dec 2, 2013 at 20:43

3 Answers 3

1

You should wrap this function in another service/factory (named filterService in example below) and inject that into the items factory, like:

backpackrApp.factory('itemsFactory', ['filterService', function(filterService) {

factory.getBackpackById = function(id) {
return backpacks.propValueFilter(function(element) {
    return element.id == id;
});
};
return factory;
}]);
Sign up to request clarification or add additional context in comments.

Comments

0

Spacing issue in the return of the 1st function? Should be:

function propValueFilter (toFilterArr, filterFunction) {
    return toFilterArr.filter(filterFunction);
};

1 Comment

sry, copypasta. there is a space char.
0

I think you want to create helper functions you can inject them from a factory (assuming you know what you're doing with Array.filter):

backpackrApp.factory('myUtil', function() {
    var obj = {
        obj.propValueFilter = function(toFilterArr, filterFunction) {
            return toFilterArr.filter(filterFunction);
        };
    };
    return obj;
});

backpackrApp.factory('itemsFactory', function(myUtil) {
    var itemsFactoryObj = {
        backpacks = []; // set somewhere
        obj.getBackpackById = function(backpacks, id) {
            return myUtil.propValueFilter(function(element) {
                return element.id == id;
            });
        };
    };
    return itemsFactoryObj;
});

backpackrApp.controller('TestCtrl', function(itemsFactory) {
    $scope.backpack = itemsFactory.getBackpackById(1);
});

Comments

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.