如何根据url参数过滤数组

kslstn

如何根据定义为true的url参数为一个属性过滤数组?我尝试了以下操作,但是:“错误:未定义$ location”。我已经将其提供给模板的控制器了。

javascript:

app.filter('myFilter', function() {

    return function( items ) {
        return items.filter(function(element){
            var ext = ($location.search().ext == "true"); // Check if url contains ?ext=true
            if (ext == true){
                if ( externallyAvailable == 'true') {
                    return true;
                }
            }
            else{
                return true;
            }
        });
    }
});

app.controller('tilesController', ['$scope', '$http', '$sce', '$location', '$filter', function($scope, $http, $sce, $location, $filter) {
    /* some controller logic */
}

模板:

<li ng-repeat="tile in list.tiles  | myFilter" class="tile-wrapper">
    <!-- content -->
</li>

我还发现,如果可行,我的解决方案将花费比我预期更多的行,因此欢迎其他建议。

乌迈尔·法鲁克(Umair Farooq)

您还需要在过滤器中注入$ location Provider。因为您还没有这样做,所以才向您显示未定义的错误。尝试这样的事情

app.filter('myFilter', function($location) {

    return function( items ) {
        return items.filter(function(element, $location){
            var ext = ($location.search().ext == "true"); // Check if url contains ?ext=true
            if (ext == true){
                if ( externallyAvailable == 'true') {
                    return true;
                }
            }
            else{
                return true;
            }
        });
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章