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

只愿长相守 提交于 2019-12-23 13:01:12

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/36209664/passing-data-from-ng-click-within-directive-into-a-function-in-the-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!