🔹 What is AngularJS?
AngularJS is a JavaScript framework developed by Google for building dynamic, single-page applications (SPAs). It extends HTML with additional attributes and binds data to HTML using expressions.
🔹 1. AngularJS Core Concepts
➤ a. Modules
A module defines the main application container.
-
'myApp'
is the application name. -
[]
is where dependencies (like routing) go.
➤ b. Controllers
Controllers are JavaScript functions attached to parts of the HTML to control the data.
➤ c. Directives
Special HTML attributes prefixed with ng-
. Examples:
Directive | Purpose | Example |
---|---|---|
ng-app | Bootstraps Angular | <html ng-app="myApp"> |
ng-model | Binds input to variable | <input ng-model="name" /> |
ng-bind | Binds data to element | <span ng-bind="name"></span> |
ng-controller | Defines controller scope | <div ng-controller="MyCtrl"> |
ng-click | Click event handler | <button ng-click="sayHi()">Click</button> |
ng-repeat | Loops through collections | <li ng-repeat="item in list">{{ item }}</li> |
Changes in the view update the model, and vice versa.
➤ e. Expressions
AngularJS expressions are written inside {{ }}
.
🔹 2. Example: Hello World AngularJS App
📄 index.html
📄 app.js
🔹 3. Forms and Validation
AngularJS provides built-in form validation.
🔹 4. Filters
Used to format data in templates.
Filter | Usage | Example |
---|---|---|
currency |
Format as currency | `{{ 123 |
uppercase |
Upper case | `{{ name |
date |
Format dates | `{{ today |
filter |
Filter arrays | `ng-repeat="x in items |
🔹 5. Services
Used to share logic/data across controllers.
Custom Service Example:
Inject in Controller:
🔹 6. Dependency Injection
AngularJS automatically injects dependencies into functions:
🔹 7. Routing (SPA)
With ngRoute
, you can change views without reloading the page.
Add Script:
App Configuration:
In HTML:
🔹 8. HTTP Requests
Using $http
for AJAX calls:
🔹 9. Custom Directives
🔹 10. AngularJS vs jQuery
Feature | AngularJS | jQuery |
---|---|---|
Type | Framework | Library |
Purpose | Build SPAs | DOM Manipulation |
Data Binding | Two-way | Manual |
MVC Support | Yes | No |
✅ Summary
Concept | Description |
---|---|
ng-app | Bootstraps the AngularJS application |
ng-model | Binds data between input and model |
ng-controller | Defines logic and data for view |
ng-repeat | Loops through arrays |
ng-click | Binds click events |
$scope | Object used to share data between view and controller |
$http | Makes AJAX requests |
$routeProvider | Configures SPA routes |
📄 index.html
(View)
📄 app/app.module.js
(Main App Module)
📄 app/controllers/hello.controller.js
(Controller)
📄 app/models/greeting.model.js
(Model)
💡 Explanation of MVC
Component | File | Role |
---|---|---|
Model | greeting.model.js | Holds and manages greeting data. |
View | index.html | HTML that binds to controller data. |
Controller | hello.controller.js | Connects model to view via $scope . |
Module | app.module.js | Registers and bootstraps app and components. |
🔹 Key AngularJS Concepts
Concept | Example | Purpose |
---|---|---|
ng-model | <input ng-model="name"> | Binds input field to model. |
ng-bind | <span ng-bind="name"></span> | Alternative to {{ name }} interpolation. |
ng-repeat | <li ng-repeat="item in items">{{ item }}</li> | Loop through a list. |
ng-click | <button ng-click="sayHello()">Click Me</button> | Binds a click handler. |
ng-show / ng-hide | <div ng-show="isVisible"> | Conditionally show/hide elements. |
🔹 AngularJS vs Angular (2+)
Feature | AngularJS (1.x) | Angular (2+) |
---|---|---|
Language | JavaScript | TypeScript |
Architecture | MVC | Component-based |
Performance | Slower | Faster |
Mobile Support | No | Yes |
Support | Deprecated | Actively maintained |
🔹 When to Use AngularJS Today
✅ Good for:
-
Maintaining existing projects
-
Legacy applications in enterprises
-
Quick internal tools/prototypes
❌ Not recommended for:
-
New production apps (use Angular 15+, React, or Vue.js)
Feature | AngularJS | Angular (2+ / latest) |
---|---|---|
Language | JavaScript | TypeScript |
Component Model | Controller + Template | Component (Class + Template + Metadata) |
Module System | Manual via script tags | NgModules with @NgModule decorator |
Dependency Injection | Basic | Hierarchical, powerful DI |
Forms | ng-model | Template-driven or Reactive Forms |
Routing | ngRoute | @angular/router |
Build Tooling | Manual or Grunt/Gulp | Angular CLI + Webpack |
MVC Concept | Angular |
---|---|
Model | Service (e.g., GreetingService ) |
View | HTML template (e.g., greeting.component.html ) |
Controller | Component class (e.g., GreetingComponent ) |
Bootstrap | Module (AppModule ) |