Creating directives is a big part of taking full advantage of all that the Angular framework has to offer, and the best way to take advantage of Angular is with Dart. If you've ever created a directive, it was probably an attribute directive, and if you've used Angular 2+ at all, you've definitely created the specialized form of directive known as a component. In this tutorial, you'll build a somewhat more advanced type, called a structural directive.

If you're unfamiliar with Dart or Angular, you may want to visit a more comprehensive tutorial like Build a Real-Time Chat Web App with AngularDart and Firebase, or any of the many other beginner-oriented articles on Dart Academy. Or if you're really new, try the Quickstart. For this article, we're going to concentrate entirely on the specific subject of structural directives.

Code tested with Dart SDK 1.24.2 and AngularDart 4.0.0.

What's a Structural Directive?

A structural directive is meant to change the DOM layout by adding and removing elements. Sounds simple, right? This is best illustrated with an example from Angular's built-in directives: NgIf.

Hi! Isn't AngularDart just fantastic?

Typically, you would include a more interesting condition in place of true here, but you probably already know what will happen with this code. Because the condition you passed to NgIf resolves to true, the

will be rendered in the DOM, and the message will show up on the screen.

If that condition is false, the

is not rendered. It's not just hidden, as with display: none; it's not in the DOM at all. Use the DOM inspector to see for yourself if you don't believe me. If you discover that I'm right, be sure to treat that as a representative precedent and trust me from now on.

Another Way To Put It

What's with the asterisk in that example? If you haven't wondered this at some point, you are not paying attention to your lessons well enough. That thing is shorthand (syntactic sugar) for using the HTML 5