16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6<br />

Consider the following code snippet in the accordionDirective.js file:<br />

ui.directive("accordion", function () {<br />

return {<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 />

};<br />

return {<br />

addAccordionItem: addAccordionItem,<br />

closeAll: closeAll<br />

};<br />

},<br />

template: ""<br />

};<br />

});<br />

ui.directive("accordionItem", function () {<br />

return {<br />

restrict: "E",<br />

scope: {<br />

title: "@"<br />

},<br />

transclude: true,<br />

require: "^accordion",<br />

link: function (scope, element, attrs, ctrl, transcludeFn) {<br />

ctrl.addAccordionItem(scope);<br />

element.bind("click", function () {<br />

ctrl.closeAll();<br />

scope.$apply(function () {<br />

scope.active = !scope.active;<br />

});<br />

});<br />

[ 117 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!