What is Single-page Application?
Characteristics
"Superheroic JavaScript MVW Framework"
http://angularjs.org
What is AngularJS?
AngularJS takes care of advanced features that users have become accustomed to in
modern web applications, such as:
Separation of application logic, data models, and views
Ajax services
Dependency injection
Testing
And more
CONCEPTS
Data Binding
How Data Binding works?
How Data Binding works?
How Data Binding works?
$scope.$watch
$scope.$watch('name', function(value) {
var firstSpace = (value || "").indexOf(' ');
if (firstSpace == -1) {
$scope.firstName = value;
$scope.lastName = "";
} else {
$scope.firstName = value.substr(0, firstSpace);
$scope.lastName = value.substr(firstSpace + 1);
}
});
How Data Binding works?
$scope.$apply([expression])
How Data Binding works?
$scope.$digest()
Modules
In Angular, a module is the main way to define an AngularJS app
Keeping our global namespace clean
Making tests easier to write and keeping them clean so as to more easily target isolated
functionality
Making it easy to share code between applications
Defining a module
<script>
var app = angular.module('myModule', ['myDependency']);
</script>
<!-- Use It->
<!DOCTYPE html>
<html ng-app="myModule">
<head></head>
<body></body>
</html>
The Angular module API allows us to declare a module using the angular.module() API method.
We can always reference our module by using the same method with only one parameter. For instance, we can reference the "myModule" module like so:
var myModule = angular.module('myModule');
Scope
angular.module('scopeExample', [])
.controller('MyController', ['$scope', function($scope) {
$scope.username = 'World';
$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}]);
<div ng-controller="MyController">
Your name:
<input type="text" ng-model="username">
<button ng-click='sayHello()'>greet</button>
<hr>
{{greeting}}
</div>
Controller
Expressions
<span>
1+2={{1+2}}
</span>
Output:
1+2=3
Filters
Directives
angular.module('moduleName')
.directive('myDirective', function () {
return {
restrict: 'EA', //E = element, A = attribute, C = class, M = comment
link: function ($scope, element, attrs) {
//DOM manipulation
alert("Alert came from myDirective");
}
}
});
Usage:
<my-directive></my-directive>
OR
<div my-directive></div>
Service
var myModule = angular.module('myModule', []);
myModule.factory('MyService', function() {
var MyService = {};
// factory function body that constructs MyService
MyService.method1 = function() {
// do something
};
return MyService;
});
Dependency Injection
Dependency Injection
AngularJS AJAX
The $http service has several functions you can use to send AJAX requests. These are:
// EXAMPLE
angular.module("myapp", [])
.controller("MyController", function($scope, $http) {
$scope.albums = [];
$http.get('/albums')
.success(function(response, status, headers, config) {
$scope.albums = response.data;
})
.error(function(response, status, headers, config) {
alert("Failed");
});
} );
AngularJS AJAX
// USAGE
$resource(url, [paramDefaults], [actions], options);
AngularJS Routes
<!DOCTYPE html>
<html lang="en">
<head>
<title>AngularJS Routes example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.min.js"></script>
</head>
<body ng-app="sampleApp">
<a href="#/route1">Route 1</a><br/>
<a href="#/route2">Route 2</a><br/>
<div ng-view></div>
<script>
var module = angular.module("sampleApp", ['ngRoute']);
module.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/route1', {
templateUrl: 'angular-route-template-1.jsp',
controller: 'RouteController'
}).
when('/route2', {
templateUrl: 'angular-route-template-2.jsp',
controller: 'RouteController'
}).
otherwise({
redirectTo: '/'
});
}]);
module.controller("RouteController", function($scope) {
})
</script>
Testing Angular application
Sample Unit Tests : https://docs.angularjs.org/guide/unit-testing
Other useful tools...
Resources: