Angular JS
Hi
As we all know that Angular JS is one of the powerful framework used to create Single page Web Applications. Demand for Angular is raising day by day.
Let’s learn something about Angular JS in this article. Hope all will enjoy.
Have a complete look on the guide below and ask questions in the Comments section if you have any.
Let’s start!
Angular JS is a JavaScript-based open-source front-end web application framework mainly maintained by Google.
Major features of Angular JS
1. An efficient framework used to create rich internet applications.
2. It provides a feature of writing the Client Side JS code using MVC Architecture.
3. It strictly follows MVC Architecture.
4. It automatically handles Cross Browser or operating system dependencies.
5. It internally has Client Side routing mechanism which enables creating single page web application.
6. It is an Open Source framework which is available for free of cost.
7. By default, it provides a feature of validating input elements using which we can easily implement form validations.
8. It is used to build page large scale, high performance and easy maintenance applications.
9. By using Angular JS, developer will achieve high or more functionalities with less piece of code.
Core Features of Angular JS:
Below are the core features of Angular.
1. Data Binding:
The process of automatic synchronization of data between Model and the view component is called as Data Binding.
It also provides two way data Binding feature under which the change in the view automatically reflects the Model.
2. Scope:
An Object which refers to the Model which acts as a glue between Controller and the Viewer. Only the data under the scope object of a particular controller can be accessible at the View Block.
3. Controllers:
It is a JS function which bounds to a particular Scope. For a single application, we can have multiple controllers.
4. Services:
Angular JS by default comes with set of built in services like $http, $location, $scope etc,.. which gets instantiated only once throughout the application. Angular even provides a feature of creating Custom Services.
5. Filters:
These select a subset of items from an array and returns the new filtered array with a condition applied.
6. Directives:
These are the markers on DOM elements such as elements, attributes etc. Angualr comes with predefined set of directives which doesn’t have any meaning through HTML. But angular adds a meaning for it.
7. Templates:
These are the rendered views with data from Controller and the Model. We create multiple templates for multiple Views according to the requirement and inject the required template to the View.
8. Routing:
In this process, we switch between the Views to implement Single page Web application feature.
Get Teja Sankuru’s stories in your inbox
Join Medium for free to get updates from this writer.
9. Dependency Injection:
Under which any required services will be injected to the Controller or other Services only if it is needed.
The architecture of Angular v1 is based on MVC whereas the architecture of Angular v2 is based on service/controller.
MVC Architecture (Model, View, Controller)
Model:
The set of instructions responsible for managing application data. It responds to the request from View block and to the instructions from the Controller to update itself.
View:
The presentation of data in a particular format trigger by the Controller’s decision to present the data.
Controller:
It responds to the User Input and performs interactions on the data model objects. The Controller receives input, validates it and then perform the business login which modifies the state of the data Model.
<html>
<head>
<title>Demo of creating an Angular Application</title>
<script type=”text/javascript” src=” — Angular.js(CDN)”></script>
<script type=”text/javascript” src=”app.js”></script>
</head>
<body ng-app=”SampleApp” ng-controller=”demoCntrl”>
<div>The sum of two numbers is {{a+b}} </br>
The User name is {{name}}
</div>
</body>
</html>app.js
var myApp = angular.module (“sampleApp”,[]);
myApp.controller(“demoCntrl”,function($scope){
$scope.a = 10;
$scope.b = 40;
//$scope.c = $scope.a + $scope.b;
$scope.name=”Mahi”;
});$http Service:
A predefined service by default available with in Angular used to create an AJAX communication within the Application.
$http({
url: ‘WebService url’,
method: ‘GET/POST’,
data: {optional data need to be passed to Server}
}). then (function(res){
//Success call back
},
function(){
//Error call back
});Implementing AJAX Communication using $http Service.
<html>
<head>
<title>Implementing AJAX Communication</title>
<link rel =”stylesheet” type=”text/css” href=”app.css”>
<script type=”text/javascript” src=”…angular.js(CDN)”></script>
<script type=”text/javascript” src=”app,js”></script>
</head>
<body ng-app=”sampleApp” ng-controller=”demoCntrl”>
<div class=”errBlock” ng-show=”isError”>
Error whiel communicating to Server. Please try again after sometime.
</div>
<div class=”product ng-repeat”=”product in ProdDetails”>
<ul>
<li>Product Name:{{product.name}}</li>
<li>Product Price:{{product.price}}</li>
<li>Product Manufacturer:{{product.manufacturer}}</li>
<li>Product discount:{{product.discount}}</li>
<li><img src = “{{product.image}}” /> </li>
</ul>
</div>
</body>
</html>app.js
var myApp = angular.module(“sampleApp”,[]);
myApp.controller(“demoCntrl”,function($scope,$http){
$scope.prodDetails = [];
$http ({url:’http://localhost:8080/07May/data.json1',
method:’GET’}).then(function(res){
$scope.isError = false;
$scope.prodDetails = res.data.products;
},function(err){
$scope.isError = true;
});
});Handling Events in Angular:
Following are the list of predefined angular directives using which we can implement even Handling.
1. ng-click,
2. ng-focus,
3. ng-mouseover
4. ng-dbclick
5. ng-keyup
6. ng-keypress
7. ng-keydown etc..
Note:
- $event is a predefined object gets generated by default when a particular even gets generated on the page.
- $event object will hold the extra information of the current event been generated.
It is not mandatory to catch $event object, it depends on the requirement.
<div ng-app= “eventApp” ng-controller=”evenController”>
<input value = “click” type=”button” ng-click=”showMsg($event)”>
</div>I will be coming with new articles.
Please stay tunned! :)







