0

I have an directive for repeat my products and it's work fine. but i want add some filter add to it.

Here is my directive code:

app.directive('product', ['$compile', function($compile) {
    return {
        restrict: 'E',
        scope: {
            products: '='
        },
        link: function(scope, element, attrs) {
            var template =
                '<ul>' +
                    '<li data-ng-repeat="product in products>' +
                        '{{product.productName}}' +
                    '</li>' +
                '</ul>';

            // Render the template.
            element.html('').append($compile(template)(scope));
        }
    }
}]);

When i add directive to my html, show me wired error!

<product products="products | filter:{mainCategoryID : category.categoryID}:true"></product>

error in console:

enter image description here

How i can fix this issue?

3
  • Can you show us your weird error! ? Commented May 28, 2016 at 6:14
  • @BABU I add screenshot of error. Commented May 28, 2016 at 6:14
  • @PraveshKhatri added Commented May 28, 2016 at 6:15

2 Answers 2

3

try this

var app = angular
  .module('MyApp', [
  ])
.controller('Main', ['$scope', function ($scope) { 
    var self = this;
  
     self.products = [{"productName":"a","id":12},{"productName":"b","id":"34"}];
}])
.directive('product', ['$compile', function($compile) {
    return {
        restrict: 'E',
        scope: {
            products: '=',
            ngModel : '=',
        },
        link: function(scope, element, attrs) {
            var template =
                '<ul>' +
                    '<li data-ng-repeat="product in products |filter:{id:ngModel}">' +
                        '{{product.productName}}' +
                    '</li>' +
                '</ul>';

            // Render the template.
            element.html('').append($compile(template)(scope));
        }
    }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <div class="main-content" ng-app="MyApp" ng-controller="Main as myCtrl">
      <div>
         <input type="text" ng-model="myCtrl.pID" />
           <product  products="myCtrl.products " ng-model="myCtrl.pID"></product>
     </div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

What is the pID?
nope. it's does't work! I want filter be changeable in html code. like that one I share on first post.
0

Instead of creating a directive. Use filter in your ng-repeat

<div data-ng-repeat="product in products | filter: checkId"></product>
   <ul>
       </li>
           {{product.productName}}
       </li>
   </ul>
<div>

In your controller

$scope.checkId = #you can do whatever you wanted to.

1 Comment

dude. I want be a directive!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.