我如何通过angularJS中的url实现按值过滤?

时间:2023-01-06 19:36:58

I did not get to realize filtering through the value obtained in the url Filter:

我没有意识到通过url过滤器中获得的值进行过滤:

$scope.categoryFilter = function (products){

                if ($routeParams.categorySlug == products.category.seo_name){
                    return;
                }else {
                    return $scope.products;
                }
            };

Routes:

when('/products/category/:categorySlug', {
            controller: 'ProductsListController',
            controllerAs: 'vm',
            templateUrl: '/static/templates/products/products_index.html'
        }).otherwise('/');

Json :

{
    "name": "pants Classic Pyjama",
    "category": {
        "id": 2,
        "name": "Белье",
        "description": "",
        "visible": true,
        "products_count": 0,
        "products_count_cache": 0,
        "category_id_name": "2",
        "seo_name": "bele",
        "seo_title": "",
        "seo_desc": "",
        "seo_keywords": "",
        "lft": 2,
        "rght": 5,
        "tree_id": 3,
        "level": 1,
        "parent": {},
    "id": 920

In template i use it like these:

在模板中我使用它像这样:

 <div class="catalog-item" ng-repeat="product in products |filter: categoryFilter">
        <div class="item-pre-title">
            Бесплатная доставка $150+
        </div>
        <div class="item-img">
            <a href="/products/{{ product.id }}/"><img ng-src="{{ product.picture[0].external_img_url }}" width="150px"
                                                       height="150px" alt=""></a>
        </div>
        <a href="/products/{{ product.id }}/" class="item-title">{{ product.name }}</a>
        <div class="item-price">
            <div class="price-old">{{ product.old_price }}</div>
            <b>{{ product.price }}</b>
        </div>
        <div class="item-footer">
            <a href="#" class="item-view"></a>
            <div class="item-sales-alert">
                Получить скиду
            </div>
        </div>
    </div>

Warning in console:

控制台警告:

angular.js:11706 TypeError: Cannot read property 'seo_name' of null

angular.js:11706 TypeError:无法读取null的属性'seo_name'

Help me to resolve it pls :)

帮我解决一下吧:)

UPD I resolve TypeErro, but filtering still does not work

UPD我解决了TypeErro,但过滤仍然无效

1 个解决方案

#1


1  

I resolve this problem adding this code instead my old Filter:

我解决了这个问题,添加此代码而不是旧的过滤器:

 $scope.categoryFilter = function (products){

                try {
                    if ($routeParams.categorySlug){
                       if ($routeParams.categorySlug == products.category.seo_name)
                            return products;
                    }else{
                        return $scope.products;
                    }
                }
                catch (TypeError) {
                   return;
                }};

#1


1  

I resolve this problem adding this code instead my old Filter:

我解决了这个问题,添加此代码而不是旧的过滤器:

 $scope.categoryFilter = function (products){

                try {
                    if ($routeParams.categorySlug){
                       if ($routeParams.categorySlug == products.category.seo_name)
                            return products;
                    }else{
                        return $scope.products;
                    }
                }
                catch (TypeError) {
                   return;
                }};