Estimated reading: 3 minutes 106 views

πŸ”§ AngularJS Tutorial – Learn AngularJS from Scratch (2025 Guide)


🌟 What is AngularJS?

AngularJS is a powerful JavaScript-based open-source front-end framework developed by Google. It allows developers to build dynamic web applications with a Model-View-Controller (MVC) architecture.

βœ… Two-way data binding
βœ… Dependency Injection
βœ… Templating engine
βœ… Great for SPAs (Single Page Applications)

πŸ”— Official AngularJS Website


βš™οΈ Setting Up AngularJS

βœ… Use CDN (Quick Start)

Add this in your HTML file:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>

πŸ§ͺ Verify

<div ng-app>
  <p>{{ 10 + 5 }}</p>
</div>

If you see 15, AngularJS is working! πŸŽ‰


πŸ“¦ AngularJS Architecture

AngularJS follows the MVC (Model-View-Controller) pattern.

  • Model – Data
  • View – UI
  • Controller – Business logic

🧩 Directives in AngularJS

Directives extend HTML with custom behavior.

DirectivePurpose
ng-appBootstraps Angular app
ng-modelBinds input fields to scope
ng-repeatRepeats HTML elements
ng-ifConditionally includes HTML
ng-clickBinds click events

πŸ”— Expressions and Data Binding

πŸ“Œ AngularJS Expression

<p>{{ 5 + 5 }}</p>

πŸ” Two-Way Data Binding

<input ng-model="name">
<p>Hello {{ name }}</p>

Changes reflect both in the model and view instantly.


πŸ“Š Controllers and Scope

<div ng-controller="MyCtrl">
  <h1>{{ message }}</h1>
</div>

<script>
  app.controller('MyCtrl', function($scope) {
    $scope.message = "Hello from Controller!";
  });
</script>

$scope connects your controller to the view.


πŸ“ Modules in AngularJS

A module is a container for different parts of your app.

var app = angular.module('myApp', []);

Everythingβ€”controllers, services, filtersβ€”resides within a module.


πŸ“ Forms and Validation

AngularJS makes form handling easy.

<form name="myForm">
  <input type="email" ng-model="user.email" required>
  <span ng-show="myForm.email.$error.required">Email is required</span>
</form>

πŸ’‘ Real-time validation is built-in!


πŸ” Filters in AngularJS

Used to format data in the view.

<p>{{ name | uppercase }}</p>

Popular filters:

  • uppercase
  • lowercase
  • currency
  • date
  • filter
  • limitTo

🧠 Services and Dependency Injection

Services are singleton objects used for logic that needs to be shared.

app.service('GreetService', function() {
  this.sayHi = function(name) {
    return "Hello " + name;
  };
});

Inject service into a controller:

app.controller('MainCtrl', function($scope, GreetService) {
  $scope.greeting = GreetService.sayHi("AngularJS");
});

πŸ”„ Routing in AngularJS

Use ngRoute module for SPA routing.

npm install angular-route
<script src="angular-route.min.js"></script>

Configure routes:

app.config(function($routeProvider) {
  $routeProvider
    .when("/", {
      templateUrl: "home.html"
    })
    .when("/about", {
      templateUrl: "about.html"
    });
});

⚑ AJAX and HTTP Services

$http.get("data.json").then(function(response) {
  $scope.myData = response.data;
});

$http service helps fetch data from APIs or servers.


🧰 Custom Directives

Build your own custom directive:

app.directive('myWelcome', function() {
  return {
    template: "<h1>Welcome to AngularJS!</h1>"
  };
});

Use as:

<my-welcome></my-welcome>

βœ… Best Practices

  • βœ”οΈ Always use ng-strict-di for dependency safety
  • βœ”οΈ Organize app by features
  • βœ”οΈ Avoid $scope where possible (use controllerAs)
  • βœ”οΈ Use services for business logic
  • βœ”οΈ Follow AngularJS Style Guide

🏁 Conclusion

AngularJS may be older, but it’s still powerful for developing robust and dynamic single-page applications. With easy binding, directives, routing, and built-in services, you can develop structured apps in no time. πŸš€


❓ FAQs

Q1: Is AngularJS still supported?
πŸ…°οΈ It reached EOL in December 2021, but it’s still used in legacy apps.

Q2: Is AngularJS the same as Angular?
πŸ…°οΈ No, Angular (2+) is a complete rewrite of AngularJS with modern features.

Q3: Can I use AngularJS for large applications?
πŸ…°οΈ Yes, but Angular (2+) is recommended for long-term scalability.

Q4: What’s the difference between ng-if and ng-show?
πŸ…°οΈ ng-if removes the element from DOM; ng-show just hides it.

Q5: Is it easy to learn AngularJS?
πŸ…°οΈ Absolutely! With simple syntax and clear concepts, it’s great for beginners.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

AngularJS Tutorial

Or Copy Link

CONTENTS
Scroll to Top