Skip to content Skip to sidebar Skip to footer

Why Doesn't Ng-click Work In My Directive And How Do I Add A Toggle Class?

I've created a directive in Angular that looks like this: angular.module('msfApp') .directive('listitem', function () { return { templateUrl: 'assets/templa

Solution 1:

1) Problem is the isolated scope. You cannot see the function in the controller scope. One solution is to pass the function reference to the directive:

http://plnkr.co/edit/GorcZZppa8qcIKbQAg2v?p=preview

<bodyng-controller="ItemController"><listitemitem="item"item-click="toggleInBasket(item)"></listitem></body>

in the directive:

scope: {
    'item': '=',
    'itemClick': '&'
}

and in the template:

<div class="tsProductAttribute" ng-click="itemClick(item)">

2) Create another function in the directive to toggle selected state and call the controller function:

angular.module('msfApp').directive('listitem', function() {
  return {
    templateUrl: 'listitem.html',
    restrict: 'E',
    scope: {
      'item': '=',
      'itemClick': '&'
    },
    link: function(scope, iElement, iAttrs) {
      scope.selected = false;
      scope.toggleState = function(item) {
        scope.selected = !scope.selected;
        scope.itemClick(item);
      }
    }
  }
});

and toggle the class in the template:

<div class="tsProductAttribute" 
    ng-class="{'tsProductAttribute--selected': selected}" 
    ng-click="toggleState(item)">

Solution 2:

This is happening because you are using isolated scopes in the directive using scope: { 'item': '=' } which creates a new scope so your ng-click is not able to bind to controller function.

Kindly refer to below link to call parent function using ng-click

calling method of parent controller from a directive in AngularJS

Solution 3:

@Macros answer made it work just fine for me! Here's my finished code:

Directive template file:

<div    class="tsProductAttribute" 
        ng-class="{'tsProductAttribute--selected': selected}" 
        ng-click="toggleState(item)">

    <spanclass="tsProductAttribute-image"><imgng-src="{{variantImage}}"></span><spanclass="tsProductAttribute-desc">{{item.productName}}</span><selectng-model="variantImage"><optionng-repeat="variant in item.variants"value="{{variant.image}}">{{variant.name}} - {{variant.listprice.amount}}</option></select><spanclass="tsProductAttribute-price">{{item.variants[0].listprice.amount}} {{item.variants[0].listprice.entity}}</span></div>

Directive:

angular.module('msfApp')
.directive('listitem', function() {
    return {
        templateUrl: 'assets/templates/directives/listitem.html',
        restrict: 'E',
        scope: {
            'item': '=',
            'itemClick': '='
        },
        link: function(scope, iElement, iAttrs) {
          scope.selected = false;
          scope.toggleState = function(item) {
            scope.selected = !scope.selected;
            scope.itemClick(item);
          }
        }
    }
});

Directive implementation:

<listitemitem="item"item-click="toggleInBasket"></listiten>

Function in the Controller:

$scope.toggleInBasket = function(item) {
        $scope.basket.toggle(item);

        console.log(basket.get());

    }

Post a Comment for "Why Doesn't Ng-click Work In My Directive And How Do I Add A Toggle Class?"