2016-04-12 22 views
1

我沒能實現通過在URL 篩選得到的值過濾:我怎麼能實現從angularJS中的URL過濾值?

$scope.categoryFilter = function (products){ 

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

路線:

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 

在模板我使用它像這些:

<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> 

警告控制檯:

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

幫我解決這個問題PLS :)

UPD I resolve TypeErro, but filtering still does not work

+0

你在哪裏使用'categoryFilter'? – kabaehr

+0

@kabaehr看模板,我編輯它 –

+0

「產品」的發佈數據。 – dfsq

回答

1

我解決這個問題,添加此代碼,而不是我的舊過濾器:

$scope.categoryFilter = function (products){ 

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