π§© AngularJS Built-in Directives (Attribute & Structural) β 2025 Guide
π§² Introduction β Why Learn AngularJS Directives?
Directives are the core building blocks of AngularJS. They allow you to extend HTML behavior by attaching custom behavior, transformations, or logic to DOM elements. AngularJS comes with a powerful set of built-in directives that make the DOM reactive and dynamic.
There are two main types of built-in directives in AngularJS:
- Structural Directives π§± β Change the DOM structure (add/remove elements)
- Attribute Directives π¨ β Change the behavior or appearance of existing elements
π― In this guide, youβll learn:
- What AngularJS directives are and how they work
- Key differences between structural and attribute directives
- How to use common built-in directives with syntax and examples
- Best practices and real-world use cases
π What Are AngularJS Directives?
A directive in AngularJS is a special token in the markup that tells the framework to do something to a DOM element (or its children).
β Basic Example:
<p ng-show="isVisible">This is a visible paragraph.</p>
Here, ng-show
is a built-in directive that shows/hides the element based on a condition.
π Types of Built-in Directives
Type | Description |
---|---|
Structural π§± | Add/remove elements from the DOM (ng-if , ng-repeat , ng-switch ) |
Attribute π¨ | Modify behavior/appearance (ng-class , ng-style , ng-model , ng-disabled ) |
π§± Structural Directives β DOM Manipulators
πΉ ng-if
Displays an element only if the expression is true.
<p ng-if="isLoggedIn">Welcome back!</p>
$scope.isLoggedIn = true;
π§Ύ If isLoggedIn
is false, the <p>
element is not present in the DOM at all.
πΉ ng-show
/ ng-hide
Shows or hides an element (keeps it in the DOM).
<p ng-show="showContent">This is shown!</p>
<p ng-hide="hideContent">This is hidden!</p>
πΉ ng-repeat
Loops over arrays/objects and creates repeated DOM elements.
<ul>
<li ng-repeat="user in users">{{ user.name }}</li>
</ul>
$scope.users = [{ name: "Alice" }, { name: "Bob" }];
π§Ύ Creates a list item for each object in the users
array.
πΉ ng-switch
Conditionally displays one element out of many.
<div ng-switch="status">
<p ng-switch-when="active">Active</p>
<p ng-switch-when="inactive">Inactive</p>
<p ng-switch-default>Unknown</p>
</div>
$scope.status = "active";
π¨ Attribute Directives β Behavior Modifiers
πΉ ng-class
Dynamically adds/removes CSS classes.
<div ng-class="{ 'active': isSelected }">Click Me</div>
$scope.isSelected = true;
πΉ ng-style
Applies inline CSS styles based on scope values.
<p ng-style="{ 'color': textColor }">Color me</p>
$scope.textColor = "green";
πΉ ng-model
Binds input values to the model with two-way binding.
<input ng-model="username">
<p>Hello, {{ username }}</p>
πΉ ng-disabled
Disables elements based on conditions.
<button ng-disabled="!formValid">Submit</button>
$scope.formValid = false;
πΉ ng-click
, ng-submit
, ng-change
These are event-based directives for interactivity.
<button ng-click="addToCart()">Add</button>
<form ng-submit="submitForm()">...</form>
<input ng-change="onChange()" ng-model="email">
π§ Differences: Structural vs Attribute Directives
Feature | Structural Directives π§± | Attribute Directives π¨ |
---|---|---|
DOM Manipulation | Yes β Add/Remove elements | No β Modify existing element only |
Affects Layout | Yes | Rarely |
Common Examples | ng-if , ng-repeat , ng-switch | ng-model , ng-class , ng-style |
Element Presence | Removes from DOM | Keeps element in DOM |
π Summary β Recap & Next Steps
AngularJS built-in directives help build interactive, responsive, and efficient applications. Structural directives manage the DOM, while attribute directives enhance behavior and style.
π Key Takeaways:
- Use structural directives to show/hide or loop elements
- Use attribute directives for styling, binding, and interactivity
ng-model
,ng-repeat
, andng-if
are most widely used
βοΈ Real-world Relevance:
Used in forms, tables, conditional UIs, lists, modals, menus, and nearly all AngularJS apps to make views dynamic and data-driven.
β FAQ β AngularJS Built-in Directives
β What is the difference between ng-if
and ng-show
?
β
ng-if
removes the element from the DOM, while ng-show
only hides it using CSS.
β Can I use ng-repeat
with objects?
β
Yes, but by default it iterates over object propertiesβnot just array elements.
β Is ng-model
available for dropdowns and checkboxes?
β
Absolutely. ng-model
works with <select>
, checkboxes, radios, inputs, and textareas.
β Are built-in directives better than custom directives?
β
Built-in directives are optimized and tested. Use them wherever possible. Custom directives are great for reusable components.
Share Now :