我正在尝试创建一个将路径与图像名称连接起来的过滤器。 我在我的应用程序中定义的值中存储图像的常量路径,如下所示:
app.value('config', { siteID: '29', apiRoot: 'http://localhost:54003/api', imageRoot: '/Content' });
现在在我的过滤器中我想注入配置对象并在我的过滤器中使用它:
(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
};
};
})()
从这样的html调用:
<img src="{{post.Icon | imageRoot}}" alt="" />
答案 0 :(得分:2)
只需从过滤器函数中删除config
参数,因为您已经可以访问内部作用域中的config
并在函数参数中指定它将为内部示波器创建一个没有赋值的新变量它。
function imageRoot(config) {
return function (imgName) { //<-- Remove from here
return config.imageRoot + '/' + imgName;
//^__ This is already accessible from the outer scope
};
};