Angular 1 is at heart a DOM compiler. We can pass it HTML, either as a template or just as a regular web page, and then have it compile an app.
We can tell Angular to treat a region of the page as an expression using the {{ }}
handlebars style syntax. Anything between the curly braces will be compiled, like so:
{{ 'Hello' + 'World' }}
This will output:
HelloWorld
We tell Angular which portion of our DOM to treat as the master template using the ng-app
directive. A directive is a custom attribute or element that the Angular template compiler knows how to deal with. Let's add an ng-app directive now:
<html>
<head>
<script src="/angular.js"></script>
</head>
<body ng-app>
{{ 'Hello' + 'World' }}
</body>
</html>
I've now told the body element to be the root template. Anything in it will be compiled.
Directives are compiler directives. They extend the capabilities of the Angular DOM compiler. This is why Misko, the creator of Angular, describes Angular as:
"What a web browser would have been had it been built for web applications.
We literally create new HTML attributes and elements, and have Angular compile them into an app. ng-app
is a directive that simply turns on the compiler. Other directives include:
ng-click
, which adds a click handler,ng-hide
, which conditionally hides an element, and<form>
, which adds additional behaviour to a standard HTML form element.Angular comes with around 100 built-in directives which allow you to accomplish most common tasks. We can also write our own, and these will be treated in the same way as the built in directives.
We build an Angular app out of a series of directives, wired together with HTML.