A Quick Intro To Angularjs
A Quick Intro To Angularjs
A Quick Intro To Angularjs
a-quick-intro-to-angularjs
AngularJS Tutorial
AngularJS is a structural framework for dynamic web apps, maintained by Google. It allows developers to use HTML as
a template language and extend HTML’s syntax to express the application’s components clearly and succinctly. The
two-way data binding and dependency injection eliminate much of the code you’d otherwise have to write.
This tutorial will introduce you to the core features of AngularJS and help you build your first AngularJS app.
Prerequisites
Before you start, you should have a basic understanding of:
HTML
JavaScript
1. Setting Up AngularJS
You can either download AngularJS or include it via CDN in your HTML file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AngularJS Tutorial</title>
<!-- Include AngularJS via CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp">
<h1>AngularJS Tutorial</h1>
<p>{{ message }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.message = "Welcome to AngularJS!";
});
</script>
</body>
</html>
1/4
a-quick-intro-to-angularjs
2. Basic Concepts
a. Directives
Directives are special tokens in the markup that tell the library to do something with a DOM element (e.g., hide it, repeat
it, etc.).
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.name = '';
});
</script>
b. Data Binding
AngularJS allows for two-way data binding, meaning that any changes to the model will reflect in the view, and vice
versa.
c. Controllers
Controllers are used to manage the scope, which is the model data that your view binds to.
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.greeting = "Hello, World!";
});
</script>
d. Filters
Filters format the value of an expression for display to the user. You can use AngularJS built-in filters such as
currency , date , limitTo , and uppercase .
2/4
a-quick-intro-to-angularjs
3. Handling Events
You can handle events like ng-click in AngularJS to respond to user interactions.
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.showMessage = function() {
$scope.message = "Button clicked!";
};
});
</script>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.user = { name: '', email: '' };
$scope.submitForm = function() {
alert("Form submitted for " + $scope.user.name);
};
});
</script>
3/4
a-quick-intro-to-angularjs
<script>
var app = angular.module('myApp', []);
app.controller('myController', function($scope, $http) {
$scope.getData = function() {
$http.get('https://jsonplaceholder.typicode.com/users')
.then(function(response) {
$scope.items = response.data;
});
};
});
</script>
In this example:
<div ng-app="myApp">
<custom-greeting></custom-greeting>
</div>
<script>
var app = angular.module('myApp', []);
app.directive('customGreeting', function() {
return {
template: "<h1>Hello from a custom directive!</h1>"
};
});
</script>
Conclusion
In this tutorial, you've learned about the core concepts of AngularJS such as directives, data binding, controllers, forms,
and HTTP requests. You can now build dynamic, single-page applications with AngularJS.
Reference
https://maduranga.com/a-quick-intro-to-angularjs
4/4