0

I am trying to create a filter that will concatenate a path with an image name. I store a constant path for images in a value defined on my app like this:

app.value('config', { siteID: '29', apiRoot: 'http://localhost:54003/api', imageRoot: '/Content' });

Now in my filter I want to inject the config object and use it in my filter:

(function () {
var app = angular.module('myApp');
app.filter('imageRoot', ['config', imageRoot]);

function imageRoot(config) {
    return function (imgName,config) {
        return config.imageRoot + '/' + imgName;  // config is undefined
    };
};

})()

Called from html like this:

<img src="{{post.Icon | imageRoot}}" alt="" />

1 Answer 1

1

Just remove config argument from filter function, because you already have access to the config in the inner scope and specifying it in the function argument will create a new variable for the innerscope with no value assigned to it.

function imageRoot(config) {
    return function (imgName) { //<-- Remove from here
        return config.imageRoot + '/' + imgName;  
               //^__ This is already accessible from the outer scope
    };
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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