π§ 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.
Directive | Purpose |
---|---|
ng-app | Bootstraps Angular app |
ng-model | Binds input fields to scope |
ng-repeat | Repeats HTML elements |
ng-if | Conditionally includes HTML |
ng-click | Binds 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 (usecontrollerAs
) - βοΈ 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 :