Introduction
AngularJS was Google's first attempt at a full front-end framework: two-way data binding, reusable components before "components" was the industry-standard word for them, and a way to extend plain HTML with new behavior. It shipped in 2010, well before React or Vue existed, and for years it was the default choice for building a dynamic web app.
Bootstrapping a page with ng-app
AngularJS attaches itself to a page through the ng-app directive. Anywhere inside the element carrying ng-app, AngularJS is watching for its own attributes and double-curly-brace expressions:
<!DOCTYPE html> <html ng-app=""> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script> </head> <body> <p>1 + 1 = {{ 1 + 1 }}</p> </body> </html>
1 + 1 = 2
Loading the AngularJS script and marking one element with ng-app is all it takes to activate the framework. Once it's running, {{ 1 + 1 }} isn't literal text anymore — it's an AngularJS expression, evaluated and replaced with its result the moment the page loads.
A minimal two-way binding example
The feature AngularJS became known for is binding an input directly to a value shown elsewhere on the page, with no event listener code written by hand:
<div ng-app=""> <input ng-model="name" placeholder="Your name"> <p>Hello, {{ name }}!</p> </div>
Typing "Maya" into the input immediately updates the paragraph to:
Hello, Maya!
— with no JavaScript event handler written anywhere. ng-model links the input's
value to the "name" expression, and {{ name }} re-renders every time it changes.ng-model="name" creates a variable called name on the current scope and keeps it in sync with the input's value on every keystroke. {{ name }} anywhere else in the same scope automatically reflects that value — this is the "two-way" part: the input updates the variable, and the variable's display updates in turn.