Angular Js Notes 1.1
Angular Js Notes 1.1
Angular JS
Introduction
• It provides the capability to create Single Page Application in a very clean and maintainable way.
• It provides data binding capability to HTML. Thus, it gives user a rich and responsive experience.
• Not Secure − Being JavaScript only framework, application written in AngularJS are not safe. Server side authentication and
authorization is must to keep an application secure.
• Not degradable − If the user of your application disables JavaScript, then nothing would be visible, except the basic page.
AngularJS - MVC Architecture
• Model View Controller or MVC as it is popularly called, is a software design pattern for developing web applications. A Model View
Controller pattern is made up of the following three parts −
• Model − It is the lowest level of the pattern responsible for maintaining data.
• View − It is responsible for displaying all or a portion of the data to the user.
• Controller − It is a software Code that controls the interactions between the Model and View.
AngularJS - MVC Architecture
• MVC is popular because it isolates the application logic from the user interface layer and supports separation of concerns. The controller
receives all requests for the application and then works with the model to prepare any data needed by the view. The view then uses the
data prepared by the controller to generate a final presentable response. The MVC abstraction can be graphically represented as follows.
AngularJS - MVC Architecture
• The Model-
The model is responsible for managing application data. It responds to the request from view and to the instructions from controller to update
itself.
• The View-
A presentation of data in a particular format, triggered by the controller's decision to present the data. They are script-based template systems
such as JSP, ASP, PHP and very easy to integrate with AJAX technology.
• The Controller
The controller responds to user input and performs interactions on the data model objects. The controller receives input, validates it, and then
performs business operations that modify the state of the data model.
AngularJS Directives
app directive
• Step 4: Bind the value of above model defined using ng-bind directive
• <p>Hello <span ng-bind = "name"></span>!</p>
Code Hello
<html>
<head>
<title>AngularJS First Application</title>
</head>
<body>
<h1>Sample Application</h1>
</body>
</html>
AngularJS - Directives
• AngularJS directives are used to extend HTML. They are special attributes starting with ng-prefix. Let us discuss the following
directives −
• ng-app − This directive starts an AngularJS Application.
• ng-init − This directive initializes application data.
• ng-model − This directive defines the model that is variable to be used in AngularJS.
• ng-repeat − This directive repeats HTML elements for each item in a collection.
• List of other AngularJS directives
ng-app directive
• The ng-app directive starts an AngularJS Application. It defines the root element. It automatically initializes or bootstraps the application
when the web page containing AngularJS Application is loaded.
• In the following example, we define a default AngularJS application using ng-app attribute of a <div> element.
• <div ng-app = ""> ... </div>
ng-init directive
• The ng-init directive initializes an AngularJS Application data. It is used to assign values to the variables. In the following example, we
initialize an array of countries. We use JSON syntax to define the array of countries.
• The ng-model directive defines the model/variable to be used in AngularJS Application. In the following example, we define a model
named name.
• <div ng-app = ""> ... <p>Enter your Name: <input type = "text" ng-model = "name"></p> </div>
ng-repeat directive
• The ng-repeat directive repeats HTML elements for each item in a collection. In the following example, we iterate over the array of
countries.
<div ng-app = ""> ... <p>List of Countries with locale:</p>
<ol>
<li ng-repeat = "country in countries"> {{ 'Country: ' + country.name + ', Locale: ' + country.locale }} </li>
</ol>
</div>
AngularJS Expressions
• <!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs
/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
</div>
</body>
</html>
AngularJS Controllers
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
var app = angular.module('myApp', []); app.controller('myCtrl',
function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
AngularJS Filters
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
</html>
AngularJS Tables
<table>
</tr>
</table>
</div>
AngularJS HTML DOM
• AngularJS has directives for binding application data to the attributes of HTML DOM elements.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</body>
</html>
Explaination
• The ng-disabled directive binds the application data mySwitch to the HTML button's disabled attribute.
• The ng-model directive binds the value of the HTML checkbox element to the value
of mySwitch.
• <form>
Check to show a header:
<input type="checkbox" ng-model="myVar">
</form>