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.

Unit Testing<br />

Creating the element with the directive<br />

The framework's compiler walks through the DOM, looking for elements that match<br />

the directives. In the following code is our directive before being compiled:<br />

<br />

Please inform the plate and the color of the car<br />

<br />

Compiling the directive<br />

After this, the element is compiled by the $compile service, returning the link<br />

function of the directive. At this moment, the template is already generated,<br />

however, we still need to call the link function, passing the $scope object in order<br />

to bind the template to the $scope service:<br />

<br />

{{topic}}<br />

<br />

<br />

<br />

Calling the link function with the scope<br />

Now, after the link function is executed, we are almost done! The {{topic}}<br />

expression could be retrieved from the $scope object; however, as we saw in Chapter<br />

5, Scope, we need to invoke the digest cycle in order to notify the expression:<br />

<br />

{{topic}}<br />

<br />

<br />

<br />

Please inform the plate and the color of the car<br />

<br />

<br />

Invoking the digest cycle<br />

Finally, we have to call the $digest function to update the view:<br />

<br />

Something went wrong!<br />

<br />

[ 130 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!