0

I create Directive for repeat my product in my app, and it's work fine. but in this directive i have ng-click and when I click on this link ng-click don't fire.

Here is my directive code:

appMainModule.directive('product', ['$compile', function($compile) {
    return {
        restrict: 'E',
        replace : true,
        terminal: true,
        scope : {
          data: '=',
          filter: '='
        },
        link: function(scope, element, attrs) {
          var filterString, template;
          if (scope.filter === 0) {
            filterString = "product in data";
          } else{
            filterString = "product in data | filter:{mainCategoryID : filter}:true"
          }
          template =
                '<div class="single" data-ng-repeat="'+filterString+'" on-finish-render="ngRepeatFinished">' +
                    '<div class="product">' +
                        '<figure>' +
                          '<span class="product__in-card" id="{{product.productID}}">06ye</span>' +
                          '<span class="product__sale" ng-show="product.promotionPrice > 0">sale</span>' +
                          '<a class="product__favorite" ng-click="productFavorite($event)"><icon p="heart"></icon></a>' +
                          '<div class="warpper-addcart">' +
                            '<a class="btn-addcart"  ng-click="addToCrd(product,true)" v-pressable><icon p="shopping-add"></icon></a>' +
                            '<a class="btn-remove-cart" ng-click="removeOneFromCrd(product,true)" v-pressable><icon p="shopping-remove"></icon></a>' +
                          '</div>' +
                          '<a ng-click="imageclick(product)" data-remodal-options="hashTracking: false">' +
                            '<img ng-src="https://swiftcow.blob.core.windows.net/productimages/{{product.imageName}}" alt="{{product.productName}}">' +
                          '</a>' +
                        '</figure>' +
                        '<a ng-click="imageclick(product)" ng-show="product.productName.length > 53"><h3>{{product.productName | limitTo: 53}} ...</h3></a>'+
                        '<a ng-click="imageclick(product)" ng-hide="product.productName.length > 53"><h3>{{product.productName}}</h3></a>' +
                        '<span class="product__price" ng-hide="product.promotionPrice > 0">{{product.currency}} {{product.price}}</span>' +
                        '<span class="product__price" ng-show="product.promotionPrice > 0">{{product.currency}} {{product.promotionPrice}}</span>' +
                        '<span class="product__weight" ng-if="product.step == 1">{{product.step}} {{product.unitType}}</span>' +
                        '<span class="product__weight" ng-if="product.step == 1">{{product.step}} {{product.unitType}}</span>' +
                        '<span class="product__weight" ng-if="product.step > 1">{{product.step}} {{product.unitType}}s</span>' +
                    '</div>' +
                '</div>';
            // Render the template.
            element.html('').append($compile(template)(scope));
        }
    }
}]);

and I call this directive in html like this:

<product data="products" filter="category.categoryID"></product>

How i can handle the ng-click in directive?

2
  • Where is your productFavorite functions within your scope? I assume it's in your parent scope. Try setting transclude to true. Commented Jun 7, 2016 at 9:49
  • @FrankerZ I add this, but we have still issue and nothing changed. Commented Jun 8, 2016 at 6:15

1 Answer 1

1

you should add controller to your directive. this controller is external controller that you use.

 return {
    restrict: 'E',
    replace : true,
    terminal: true,
    scope : {
      data: '=',
      filter: '='
    },
    controller:"SomeController as ctrl" 

and in template change to this:

   '<a class="product__favorite" ng-click="ctrl.productFavorite($event)"><icon p="heart"></icon></a>' +

Demo

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

7 Comments

did you see my answer?
I try this. and add my controller name as like you. but show me undefined error in console.
do you use controllerAs syntax in your controller and view?
What do you mean?!
can you create plunker?
|

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.