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.

Creating Reusable Components with Directives<br />

templateUrl<br />

There is another way to achieve the same goal with more quality. We just need<br />

to move the HTML snippet to an isolated file and bind it using the templateUrl<br />

property, as shown in the following code snippet:<br />

index.html<br />

<br />

directives.js<br />

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

return {<br />

templateUrl: "alert.html"<br />

});<br />

alert.html<br />

<br />

<br />

Something went wrong!<br />

<br />

<br />

You must inform the plate and the color of the car!<br />

<br />

<br />

replace<br />

Sometimes it might be interesting to discard the original element, where the directive<br />

was attached, replacing it by the directive's template. This can be done by enabling<br />

the replace property:<br />

directives.js<br />

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

return {<br />

templateUrl: "alert.html",<br />

replace: true<br />

};<br />

});<br />

[ 36 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!