Answer the question
In order to leave comments, you need to log in
How to work with controller in directive?
Something I'm stupid in the morning.
I make a directive that should produce a list received from the service
(function () {
'use strict';
angular
.module('admin')
.directive('addToCart', addToCart);
function addToCart() {
var directive = {
link: link,
template: '<div ng-repeat="item in items">{{item.name}}</div>',
restrict: 'EA',
controller: AddToCartController,
conrollerAs: 'addto'
};
return directive;
function link(scope, element, attrs,ctrl) {
}
AddToCartController.$inject = ['Product'];
function AddToCartController(Product) {
var vm = this;
vm.items = Product.query();
}
}
})();
vm.items
$scope.items = vm.items;
to the link
Answer the question
In order to leave comments, you need to log in
you don't need a link here. The only thing you need here is to specify an isolated scope so that there are no conflicts with other directives.
return {
template: '<div ng-repeat="item in items">{{item.name}}</div>',
restrict: 'EA',
controller: AddToCartController,
conrollerAs: 'addto',
scope: {}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question