Lecture#08 Angular JS
Lecture#08 Angular JS
</div>
Ng-init.html
Alternatively above can be written as:
<div data-ng-app="" data-ng-init="firstName=‘Mubbashir'">
</div>
AngularJS Expressions
You can write expressions wherever you like, AngularJS will simply resolve the expression
and return the result.
Example: Let AngularJS change the value of CSS properties.
Change the color of the input box below, by changing its value:
<div ng-app="" ng-init="myCol='lightblue'">
<input type=“text” style="background-color:{{myCol}}" ng-
model="myCol">
</div>
Example CSS.html
AngularJS Numbers
</div>
AngularJS Strings
<div ng-app="" ng-init="firstName=‘Mubbashir';lastName=‘Ayub'">
</div>
AngularJS Objects
<div ng-app="" ng-
init="person={firstName:'Mubbashir',lastName:'Ayub'}">
</div>
AngularJS Arrays
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
Same example using ng-bind:
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
AngularJS Expressions vs. JavaScript Expressions
Like JavaScript expressions, AngularJS expressions can contain literals, operators, and
variables.
Unlike JavaScript expressions, AngularJS expressions can be written inside HTML.
AngularJS expressions do not support conditionals, loops, and exceptions, while JavaScript
expressions do.
AngularJS expressions support filters, while JavaScript expressions do not.
AngularJS Modules
An AngularJS module defines an application.
The module is a container for the different parts of an application.
The module is a container for the application controllers.
Creating a Module
A module is created by using the AngularJS function angular.module
<div ng-app="myApp">...</div>
<script>
</script>
The "myApp" parameter refers to an HTML element in which the application will run.
Adding a Controller
Add a controller to your application, and refer to the controller with the ng-controller
directive:
<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div> The [] parameter in the module definition can
be used to define dependent modules.
<script> Without the [] parameter, you are
not creating a new module, but retrieving an
var app = angular.module("myApp", []);existing one.
app.controller("myCtrl", function($scope) {
$scope.firstName = “Mubbashir";
$scope.lastName = “Ayub";
});
</script>
Adding a Directive
AngularJS has a set of built-in directives which you can use to add functionality to your
application.
In addition you can use the module to add your own directives to your applications:
<div ng-app="myApp" w3-test-directive></div>
<script>
var app = angular.module("myApp", []);
app.directive("w3TestDirective", function() {
return {
template : "I was made in a directive constructor!"
};
});
</script>
Example: Directive.html
Modules and Controllers in Files
It is common in AngularJS applications to put the module and the controllers in separate
JavaScript files.
In this example, "myApp.js" contains an application module definition, while "myCtrl.js"
contains the controller.
Example: Files.html
AngularJS Applications
AngularJS modules define AngularJS applications.
AngularJS controllers control AngularJS applications.
The ng-app directive defines the application, the ng-controller directive defines the controller.
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
AngularJS module defines
</div> application:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "Mubbashir";
$scope.lastName= "Ayub"; AngularJS controllers control
});
applications:
</script>
Repeating HTML Elements
Data binding in AngularJS is the synchronization between the model and the view.
When data in the model changes, the view reflects the change, and when data in
the view changes, the model is updated as well. This happens immediately and
automatically, which makes sure that the model and the view is updated at all times.
AngularJS Controllers
AngularJS controllers control the data of AngularJS applications.
AngularJS controllers are regular JavaScript Objects.
The ng-controller directive defines the application controller.
A controller is a JavaScript Object, created by a standard JavaScript object constructor.
<div ng-app="myApp" ng-controller="myCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = “Mubbashir";
$scope.lastName = “Ayub";
});
</script>
Example: Controller.html
Explanation
The AngularJS application is defined by ng-app="myApp". The application runs inside
the <div>.
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller.
The myCtrl function is a JavaScript function.
AngularJS will invoke the controller with a $scope object.
In AngularJS, $scope is the application object (the owner of application variables and
functions).
The controller creates two properties (variables) in the scope (firstName and lastName).
The ng-model directives bind the input fields to the controller properties (firstName and
lastName).
Controller Methods
The example above demonstrated a controller object with two properties: lastName and firstName.
A controller can also have methods (variables as functions):
<div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = “Mubbashir";
$scope.lastName = “Ayub";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>
Example: ControllerMethods.html
AngularJS Scope
The scope is the binding part between the HTML (view) and the JavaScript (controller).
The scope is an object with the available properties and methods.
The scope is available for both the view and the controller.
How to Use the Scope?
When you make a controller in AngularJS, you pass the $scope object as an argument:
app.controller('myCtrl', function($scope)
$scope.carname = "Volvo";
When adding properties to the $scope object in the controller, the view (HTML) gets
access to these properties.
In the view, you do not use the prefix $scope, you just refer to a property name, like
{{carname}}.
Understanding the Scope