angularjsangularjs-directiveangularjs-scopeisolate-scope

Passing data from ng-click within directive into a function in the controller


I found this question which gets me almost to where I need to be. Why doesn't ng-click work in my directive and how do I add a toggle class?

Which makes it so my ng-click within my directive template triggers a function in my controller. http://plnkr.co/edit/GorcZZppa8qcIKbQAg2v?p=preview

The issue is that the parameter returned to my controller (item) is undefined. I need this to actually pass data from a variable within my directive to be used in the function that I will run in the controller.

Directive template file

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

    <span class="tsProductAttribute-image">
        <img ng-src="{{variantImage}}">
    </span>
    <span class="tsProductAttribute-desc">{{item.productName}}</span>
    <select ng-model="variantImage">
        <option  ng-repeat="variant in item.variants" value="{{variant.image}}">{{variant.name}} - {{variant.listprice.amount}}</option>
    </select>
    <span class="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

<listitem item="item" item-click="toggleInBasket(item)"></listitem>

Function in controller

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

        console.log(basket.get());

    }

(item) is undefined


Solution

  • While passing function to directive isolated scope, you should be using &(expression binding) to pass method reference. On item-click you should mentioned actual call to controller method like toggleInBasket(item)

    Markup

    <listitem item="item" item-click="toggleInBasket(item)"></listitem>
    

    And then while calling method from directive you should call it as scope.itemClick({item: item})

    Directive

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

    Demo here