Answer the question
In order to leave comments, you need to log in
How to use ng-click in a directive with a controller?
All the same, directives kill me. We need to achieve understanding.
I do this on the page
<div ng-repeat="item in ctrl.products">
<add-to-cart item="item.name"></add-to-cart>
</div>
(function () {
'use strict';
angular
.module('admin')
.directive('addToCart', addToCart);
function addToCart() {
var directive = {
link: link,
scope: {
'item': '=',
'itemClick': '&'
},
transclude: true,
template: '<div>{{item}}<button ng-click="addItem()">+</button></div>',
restrict: 'EA',
controller: AddToCartController
};
return directive;
function link(scope, element, attrs, ctrl) {
}
AddToCartController.$inject = [];
function AddToCartController() {
var vm = this;
vm.addItem = function (item) {
alert(item);
};
}
}
})();
vm.addItem
<add-to-cart item="item.name" item-click="addItem(item)" ></add-to-cart>
- does not work. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question