Why Called Angularjs
Why Called Angularjs
JavaScript. It can be added to an HTML page with a <script> tag. AngularJS, is an open-
AngularJS is quite new Technology its first version 1.0 was released in 2012. It is developed
by Misko Hevery, a Google employee, he started to work on AngularJS in 2009. Its latest
version is 1.2.21.
AngularJS Directives
ng-model : ng-model directive binds the value of HTML controls (input, select,
textarea) to application data.
Because HTML has Angular brackets and "ng" sounds like "Angular".
and compatible with both desktop and mobile browsers. So it's definitely not a plugin or some
Angularjs Features
It is framework of JavaScript and it have own event handing directives. It is open source and it
is free for every one. AngularJS have a lot of new features which is given below;
Two Way Data-Binding
MVC
AngularJS has a built-in dependency injection subsystem that helps the developer by making
Validation
AngularJS provides client side validation same like JavaScript. Using AngularJS you can
Filter
Filter are mainly used for modify the data. Filters can be added to expressions and directives
Directives
A directive is something that introduces new syntax. It improve the feature or functionality of
html elements. Directives are markers on a DOM element which attach a special behavior to
it. For example, static HTML does not know how to create and display a date picker widget.
To teach HTML this new syntax we need a directive. AngularJS directives are extended
Forms
An AngularJS form is a collection of input controls like button, input elements. AngularJS has
some features for binding data of HTML form input fields to the model object ($scope). You
Advantage of Angularjs
Less Code: It help to write less code. Using angularjs developers can achieve
more functionality with writing less code.
Testable: It make your application testable. AngularJS code is unit testable.
It increase the level of abstraction.
AngularJS support most of all new web browser and all mobiles OS.
In AngularJS view are pure html code and controller are written in JavaScript
to achieve business logic.
Dis-advantage of Angularjs
AngularJS have lot of demerits here we will discuss some important dis-advantages;
Angularjs Directives
A directive is something that introduces new syntax. It improve the feature or functionality of
html elements. Directives are markers on a DOM element which attach a special behavior to
it. For example, static Html does not know how to create and display a date picker widget. To
teach Html this new syntax we need a directive. AngularJS directives are extended Html
ng-app
AngularJS that the <div> element is the "owner" of the AngularJS application. Using this
directive you can tell which part of html contains Angularjs app. Below we use ng-app with
<div> tag.
Example
<div ng-app="">
Enter text <input type="text" ng-model="name">
<p ng-bind="name"></p>
</div>
Result
Enter text
ng-init
The ng-init directive initializes an AngularJS Application data. It is used to assign values to the
variables.
Example
Try it yourself
ng-model
The ng-model directive binds the value of Html controls (input, select, textarea) to application
data. The ng-model directive defines the model/variable to be used in AngularJS Application.
<div data-ng-app="">
1st number <input type="number" ng-model="num1">
2nd number <input type="number" ng-model="num2">
<p><b>Sum:</b> {{num1 + num2}}</p>
</div>
Try it yourself
ng-repeat
The ng-repeat directive repeats an Html element. ng-repeat directive repeats a specific
element.
Example
Try it yourself
Angularjs Controller
AngularJS applications are controlled by Controller. AngularJS controllers are used for
control the data of AngularJS applications. AngularJS controllers are regular JavaScript
Objects.
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
</div>
<script>
function AJSController($scope) {
$scope.firstName = "Harry",
$scope.lastName = "Porter"
}
</script>
</body>
</html>
Result
First Name:
Last Name:
In above example controller are use for conrtoller firstName and lastName. AngularJS will
Expressions
AngularJS Expressions used to binds application data to HTML. AngularJS expressions are
written inside double curly braces such as : {{ expression }}. Angularjs Expressions behave
same like to ng-bind directives. AngularJS expressions are pure JavaScript expressions and
Calculator
First Number
Second Number
Sum: 11
Sub: 9
Mul: 10
Div: 10
Example
<!DOCTYPE html>
<html lang="en">
<div ng-app="" ng-init="num1=10; num2=1">
<h2>Calculator</h2>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
</body>
</html>
Note: when you remove the ng-app directive, Html will display the expression as it is, without
solving it.
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
</div>
</body>
</html>
MVC Architecture
MVC stand for Model View Controller. Model View Controller is a software design pattern for
developing web application. It given software application into three interconnected parts.
Model is responsible for maintaining data. Model retrieve data form database and also store
data in database.
View
Controller
Controller It is responsible for controls the interactions between the Model and View.
Controllers can read data from a view, control user input, and send input data to the model.
Html DOM
AngularJS has directives for binding application data to the attributes of HTML DOM
elements. The following directives are used to bind application data to the attributes of HTML
DOM elements;
S.No. Name Description
ng-disable Directive
ng-disable directive are use for binds AngularJS application data to the disabled attribute of
HTML elements.
Example
<div ng-app="">
<input type="checkbox" ng-model="enableDisableButton">Disable Button
<button ng-disabled="enableDisableButton">Click Me!</button>
</div>
Result
Disable Button
Click Me!
ng-show Directive
<div ng-app="">
<input type="checkbox" ng-model="hideme">Show Button
<button ng-show="hideme">Click Me!</button>
</div>
ng-hide Directive
ng-hide directive are use for hide or show an Html elements.
Example
<div ng-app="">
<input type="checkbox" ng-model="hideme">Hide Button
<button ng-hide="hideme">Click Me!</button>
</div>
ng-click Directive
</div>
<script>
function clickController($scope) {
$scope.count = 0;
}
</script>
Angularjs Filters
Filter are mainly used for modify the data. Filters can be added to expressions and directives
uppercase
uppercase filter are used for converting lower case text to upper case text. It is use to
Example
<script>
function personController($scope) {
$scope.name="Porter"
}
</script>
Example
Enter first name:
lowercase
lowercase filter are used for converting upper case text to lower case text. It is use to
Example
<script>
function personController($scope) {
$scope.name="PORTER"
}
</script>
currency
Add currency filter to an expression returning number using pipe character. Below we print
Example
</div>
<script>
function personController($scope) {
$scope.quantity=10;
$scope.price=1;
}
</script>
Angularjs Forms
An AngularJS form is a collection of input controls like button, input elements. AngularJS has
some features for binding data of HTML form input fields to the model object ($scope). You
Html Controls
select elements
button elements
textarea elements
<script>
function formController ($scope) {
$scope.firstName="Harry";
$scope.lastName="Porter";
}
</script>
Output
First Name:
Last Name:
Angularjs Include
Using AngularJS, we can include or embed Html pages within an Html page using ng-include
directive.
Html does not support include or embed Html pages within the html page. To achieve this
Ajax :Use Ajax to fetch data from a server, and then write the data to the inner
HTML of an HTML element.
Server Side Technologies: JSP, PHP and some other server side
technologies are use for include or add Html page within a dynamic page.
Using AngularJS, we can include or embed Html pages within an Html page using ng-include
directive.
Example
<div class="container">
<div ng-include="'header.html'"></div>
<div ng-include="'footer.html'"></div>
</div>
Angularjs Table
The ng-repeat directive is use for draw tables easily. Table data are generally repeatable.
Example
<table>
<tr ng-repeat="x in names">
<td>{{ x.Student}}</td>
<td>{{ x.Marks}}</td>
</tr>
</table>
You can also style your table using CSS same like below.
Example
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
<html>
<head>
<title>AngularJS Table</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<h2>AngularJS Application Using Table</h2>
<div ng-app="" ng-controller="studentController">
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr ng-repeat="subject in student.subjects">
<td>{{ subject.name }}</td>
<td>{{ subject.marks }}</td>
</tr>
</table>
</div>
<script>
function studentController($scope) {
$scope.student = {
subjects:[
{name:'Physics',marks:60},
{name:'Chemistry',marks:70},
{name:'Math',marks:65},
{name:'English',marks:62},
{name:'Hindi',marks:67}
],
};
}
</script>
</body>
</html>
Result
Physics 60
Chemistry 70
Math 65
English 62
Hindi 67
Angularjs Validation
An AngularJS forms and input control can validate input data. It is 100% JavaScript so
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<h2>Client side Validation Example</h2>
<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>
<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<input type="submit"
ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
myForm.email.$dirty && myForm.email.$invalid">
</p>
</form>
<script>
function ajvalidater($scope) {
$scope.user = 'Harry Porter';
$scope.email = '[email protected]';
}
</script>
</body>
</html>
Output
Username:
Email:
Submit
Note: The HTML form attribute novalidate is used to disable default browser validation.
Interview Question
Scope
Controller
Model
View
Services
Data Binding
Directives
Filters
Testable
scope is an object that refers to the application model. It is an execution context for
expressions. Scopes are arranged in hierarchical structure which mimic the DOM
structure of the application. Scopes can watch expressions and propagate events.
MVC pattern
Applying Animations
Event Handlers
Read more......
Because HTML has Angular brackets and "ng" sounds like "Angular".
Yes, Angular can use jQuery if it's present in your app when the application is
being bootstrapped. If jQuery is not present in your script path, Angular falls back
to its own implementation of the subset of jQuery that we call jQLite.
Because HTML has Angular brackets and "ng" sounds like "Angular".
AngularJS mostly support all new web browser, following browsers supported by
angularJS: Safari, Chrome, Firefox, Opera, IE8, IE9 and mobile browsers
(Android, Chrome Mobile, iOS Safari).
The ng-init directive initialize application data same like variable initialization in C
language, In c language you initialize int a=10;.
The ng-model directive binds the value of HTML controls (input, select, textarea)
to application data. The ng-model directive defines the variable to be used in
AngularJS Application.
Filter are mainly used for modify the data. Filters can be added to expressions and
directives using a pipe (|) character. For example uppercase filter are used for
converting lower case text to upper case text. Read more......
Using AngularJS, we can include or embed Html pages within an Html page using
ng-include directive. Read more......
AngularJS have its own event directive to handle DOM events like mouse clicks,
moves, keyboard presses, change events etc. Read more......
MVC Architecture
What is MVC Architecture ?
MVC stand for Model View Controller. Model View Controller is a software design
pattern for developing web application. It given software application into three
interconnected parts, model, view and controller.
Model is responsible for maintaining data. Model retrieve data form database and
also store data in database.
MVC Architecture
MVC stand for Model View Controller. Model View Controller is a software design pattern for
developing web application. It given software application into three interconnected parts.
Model is responsible for maintaining data. Model retrieve data form database and also store
data in database.
View
Controller
Controller It is responsible for controls the interactions between the Model and View.
Controllers can read data from a view, control user input, and send input data to the model.