</>
Skip to content
AngularJS lessons (9/28)

AngularJS — Directives

Built-in Directives

DirectiveDescription
ng-appRoot element
ng-controllerController
ng-modelTwo-way binding
ng-repeatLoop through items
ng-show/ng-hideShow/hide elements
ng-ifAdd/remove from DOM
ng-clickClick handler
ng-classAdd/remove classes
ng-styleInline styles

ng-repeat

<ul>
  <li ng-repeat="item in items">{{item.name}}</li>
</ul>

<!-- With index -->
<li ng-repeat="item in items track by $index">
  {{$index + 1}}. {{item.name}}
</li>

ng-show and ng-hide

<div ng-show="isVisible">Visible when true</div>
<div ng-hide="isVisible">Hidden when true</div>

ng-if

<div ng-if="isLoggedIn">Welcome back!</div>
<div ng-if="!isLoggedIn">Please log in</div>

ng-class

<div ng-class="{active: isActive, highlight: isHighlighted}">
  Content
</div>

ng-click

<button ng-click="count = count + 1">Click me</button>
<p>Count: {{count}}</p>

Custom Directive

app.directive('myDirective', function() {
  return {
    restrict: 'E',
    template: '<div>{{text}}</div>',
    scope: {
      text: '@'
    }
  };
});

Mini Practice

  1. Use ng-repeat
  2. Show/hide elements
  3. Apply conditional classes
  4. Create a custom directive

Up Next

Continue with Events — AngularJS events.

Related Topics

Frequently Asked Questions about Directives

What is Directives in AngularJS?

Directives is a fundamental concept in AngularJS. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Directives?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Directives.

Why is Directives important in AngularJS?

Directives is essential for AngularJS development. Understanding this concept will help you write better code and solve real-world problems more effectively.