AngularJS Essentials
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Creating Reusable Components with Directives<br />
title: "@"<br />
},<br />
transclude: true,<br />
require: "^accordion",<br />
link: function (scope, element, attrs, ctrl, transcludeFn) {<br />
element.bind("click", function () {<br />
scope.$apply(function () {<br />
scope.active = !scope.active;<br />
});<br />
});<br />
}<br />
};<br />
});<br />
Now, we need to define the controller inside the accordion directive; otherwise, an<br />
error will be thrown that says the controller can't be found.<br />
controller<br />
The controller is pretty similar to the link function and has almost the same<br />
parameters, except itself. However, the purpose of the controller is totally different.<br />
While it is recommended that you use the link to bind events and create behaviors,<br />
the controller should be used to create behaviors that will be shared with other<br />
directives by means of the require property:<br />
directives.html<br />
parking.directive("accordion", function () {<br />
return {<br />
template: "",<br />
restrict: "E",<br />
transclude: true,<br />
controller: function ($scope, $element, $attrs, $transclude) {<br />
var accordionItens = [];<br />
var addAccordionItem = function (accordionScope) {<br />
accordionItens.push(accordionScope);<br />
};<br />
var closeAll = function () {<br />
angular.forEach(accordionItens, function (accordionScope) {<br />
accordionScope.active = false;<br />
});<br />
[ 46 ]<br />
www.it-ebooks.info