16.07.2017 Views

AngularJS Essentials

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7<br />

Directives<br />

The last kind of component that we are going to test is the directive. Let's start with<br />

one of the directives we created in Chapter 2, Creating Reusable Components with<br />

Directives, and converted it into a separated module in Chapter 6, Modules:<br />

uiApp.js<br />

var ui = angular.module("ui", []);<br />

alertDirective.js<br />

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

return {<br />

restrict: "E",<br />

scope: {<br />

topic: "@"<br />

},<br />

replace: true,<br />

transclude: true,<br />

template:<br />

"" +<br />

"" +<br />

"{{topic}}" +<br />

"" +<br />

"" +<br />

"" +<br />

""<br />

};<br />

});<br />

The directive is by far the most complex component to be tested. Part of the<br />

complexity comes from the framework's life cycle, which we should understand<br />

before creating any directive specification. In the following section, you will<br />

understand this, step by step.<br />

[ 129 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!