[英]Pass part of the URL to a custom filter in AngularJS
I have a database in JSON, which contents I am displaying with a controller and ng-repeat. 我有一个JSON数据库,正在使用控制器和ng-repeat显示这些内容。
It works just fine. 它工作正常。
In the list I have items that belong to multiple categories. 在列表中,我有属于多个类别的项目。
I have built a custom filter to only display the stuff that is matching the criteria. 我建立了一个自定义过滤器,仅显示符合条件的内容。
app.filter('whichCategory', function($location){
return function(input) {
var out = [];
var path = $location.path();
var pos = path.lastIndexOf("/");
var categoryName = path.slice(-pos);
angular.forEach(input, function(items) {
if (items.category == categoryName) {
out.push(recipes);
}
})
return out;
}
});
The filter works as well with a static criteria or even with categoryName but in that case only brings up the elements from one category, regardless of the url. 过滤器也适用于静态条件,甚至适用于categoryName,但在这种情况下,无论网址如何,都仅显示一个类别中的元素。
Any idea where was I mistaken? 知道我在哪里弄错了吗?
In the meantime I have done some debugging, and the problem is with the pos variable implementation when telling splice which side to cut from. 同时,我已经进行了一些调试,并且当告诉splice要从哪一侧剪切时,问题在于pos变量的实现。
I was going right to left instead of the other way around. 我是从右向左走,而不是相反。
printf debugging helped :D. printf调试帮助:D。
var categoryName = path.slice(pos+1);
Because the position of the last "/" character is the 11th from the left. 因为最后一个“ /”字符的位置是从左数第11个。
I need everything after the last "/" hence the +1 我需要最后一个“ /”之后的所有内容,因此需要+1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.