AngularJS (Angular 1.x) is a JavaScript-based structural framework developed by Google for building dynamic single-page applications (SPAs). It extends HTML's syntax with directives and enables two-way data binding between models and views, allowing the DOM to update automatically when data changes. AngularJS 1.8.3 (released December 2021) is the final official version, having reached end-of-life on December 31, 2021, yet it remains widely encountered in legacy codebases. Understanding AngularJS's declarative approach β where you describe what the UI should do rather than how to manipulate the DOM β is crucial for maintaining existing applications and recognizing the patterns that influenced modern frameworks.
What This Cheat Sheet Covers
This topic spans 19 focused tables and 204 indexed concepts, 110 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Directives - Data Binding and Display
These are the directives you reach for constantly β they connect your data to the DOM and decide what shows up on screen. From ng-model's two-way binding to the conditionals (ng-if, ng-show, ng-switch) and the repeaters (ng-repeat, ng-options), this is the everyday vocabulary of declarative templating in AngularJS, where you describe the result and let the framework keep the view in sync as data changes.
| Directive | Example | Description | |
|---|---|---|---|
<input ng-model="username"> | β’ Binds input, select, or textarea value to a scope property β’ enables two-way data binding where changes in view update model and vice versa | ||
<span ng-bind="message"></span> | β’ One-way binds scope expression to element's text content β’ faster initial render than {{ }} interpolation as it avoids brief flash of uncompiled markup | ||
<div ng-bind-html="trustedHtml"></div> | β’ Binds and renders HTML content from scope expression β’ requires ngSanitize module or $sce.trustAsHtml() to mark content safe | ||
<h1>{{title}}</h1> | β’ Embeds expressions directly in HTML β’ supports filters and one-time binding with {{::expression}} prefix | ||
<li ng-repeat="item in items"> {{item.name}}</li> | β’ Instantiates template once per item in collection β’ creates child scope for each iteration with $index, $first, $last, $even, $odd properties | ||
<select ng-model="sel" ng-options="x.name for x in items"></select> | β’ Dynamically generates <option> elements for a <select> from array or objectβ’ more efficient than ng-repeat on <option> | ||
<div ng-if="isVisible"> Content</div> | β’ Adds or removes element from DOM based on expression β’ creates new scope when true; better performance for heavy conditional content than ng-show | ||
<p ng-show="isActive">Text</p> | β’ Shows or hides element by adding/removing .ng-hide CSS class (display: none)β’ element remains in DOM; faster toggling than ng-if | ||
<p ng-hide="isHidden">Text</p> | β’ Hides or shows element β’ inverse of ng-show; applies .ng-hide when expression is true | ||
<div ng-switch="status"> <p ng-switch-when="active">Active</p> <p ng-switch-default>Other</p></div> | β’ Conditionally swaps DOM structure based on expression β’ uses ng-switch-when for cases and ng-switch-default for fallback | ||
<ng-pluralize count="itemCount" when="{'0':'No items','1':'1 item','other':'{} items'}"></ng-pluralize> | Displays locale-aware plural message based on numeric count using en-US pluralization rules | ||
<div ng-class="{active: isActive, disabled: !isEnabled}"> | β’ Dynamically adds/removes CSS classes β’ accepts object, string, or array; evaluates on each digest cycle | ||
<div ng-style="{color: textColor, 'font-size': fontSize}"> | β’ Dynamically sets inline styles β’ accepts object with CSS properties; property names with hyphens must be quoted | ||
<button ng-disabled="isLoading">Save</button> | Sets the disabled attribute on form controls if expression is truthy | ||
<input ng-model="val" ng-readonly="locked"> | β’ Sets the readonly attribute on inputsβ’ special directive required since interpolation inside readonly is not allowed | ||
<input type="checkbox" ng-checked="isSelected"> | β’ Sets checked attribute if expression is truthyβ’ do not combine with ng-model β use ng-model for two-way binding instead | ||
<img ng-src="{{imageUrl}}"> | β’ Sets src attribute after expression evaluatesβ’ prevents browser from requesting literal {{imageUrl}} before Angular compiles | ||
<a ng-href="{{profileUrl}}">Profile</a> | β’ Sets href attribute after expression evaluatesβ’ prevents broken links during compilation | ||
<div ng-include="'template.html'"></div> | β’ Fetches, compiles, and includes external HTML template β’ creates new child scope; value must be expression evaluating to URL | ||
<div ng-cloak>{{content}}</div> | β’ Hides element during Angular compilation β’ prevents flashing of uncompiled content on page load; removed when compilation completes | ||
<span ng-non-bindable>{{raw}}</span> | Tells Angular not to compile or bind the element's contents β useful for displaying Angular syntax as literal text |