Unit 2
Unit 2
subject code:202046701
Unit-2
AngularJS website -
https://angularjs.io
https://angularjs.org/
3
Setup AngularJS Development Environment
• Use any web server such as IIS, apache etc., locally for
development purpose.
We generally use this directive at root level like inside html tag or in body tag.
When angularjs application loads, it will first search for ng-app directives.
ng-model directive
Ng-model is used to bind input fields, select and textarea with model
and we can use this model in page.
AngularJ <body>
<div ng-app="">
S <p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
Example </div>
</body>
</html>
• AngularJS starts automatically when the web
page has loaded.
• The ng-app directive tells AngularJS that
AngularJ the
<div> element is the "owner" of an AngularJS
S application.
• The ng-model directive binds the value of the
Example input field to the application variable name.
d variable name.
• ng-app directive indicates the start of
AngularJS application.
S <div ng-app=“”>
<p>The value of 5 times 10 is : {{5*10}}<p>
Numbers </div>
• Strings can be initialized using ng-init directive or ng-
controller directive.
• The concatenation of strings is also possible when the +
operator is used within the expression.
• Strings also could be used as expressions within double
</body>
</html>
• AngularJS application mainly relies on controllers to
control the flow of data in the application.
• A controller is defined using ng-controller directive.
• Each controller accepts $scope as a parameter which
AngularJS refers to the application/module that controller is to
control.
Controllers • Syntax:
<div ng-app = "" ng-controller = "controller_name">
//code
</div>
• Example:
(JavaScript • Syntax:
Function) function controllerAsFunction($scope){
//definition of controller
}
AngularJS Controllers Example
2. Create an App
Module
AngularJS model="lastName"><br>
Full Name: {{firstName + " " +
lastName}}
Controllers </div>
Example • <script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= ”Rambo";
});
Restrictions on Controller in
AngularJS
• Following are the scenarios where a controller can’t be used:
• Sharing code or state across
• Filtering output – Instead, there are various built-in filters by AngularJS as
well as custom filters.
• Manipulating DOM (Document Object Model) – Data binding or directives
exist for this purpose.
• Formatting input – Angular form controls are there for formatting
purposes.
Example: Handle Button Click
Attach
Behavio
r
Attach
Object
Nested
Controller
s
Difference between AngularJS
Expressions and JavaScript Expressions
AngularJS Expressions JavaScript Expressions
• AngularJS expressions can be written • JavaScript expressions cannot be
inside HTML. written inside HTML.
Method Description
$eval() Executes the expression on the current scope and returns the
result.
$apply() Executes an expression in angular outside the angular framework.