Tutorial 5.javascript
Tutorial 5.javascript
Concepts
Contd…
TUTORIAL 5
Arrays
<SCRIPT <SCRIPT
LANGUAGE= LANGUAGE=
"JavaScript"> "JavaScript">
document.write("1");
document.write("2");
for (i=1; i<=5; i++)
document.write("3");
document.write(i);
document.write("4");
document.write("5");
</SCRIPT>
Functions
function sum_of_squares(num1,num2)
{return (num1*num1) + (num2*num2);}
function sum_of_squares(num1,num2)
{return (square(num1) + square(num2));}
Why AngularJS
<html ng-app>
<head>
<script src='angular.js'></script>
</head>
<body>
<input ng-model='user.name'>
<div ng-show='user.name'>Hi {{user.name}}</div>
</body>
</html>
MVC
Notifies
Changes
Controller
(Logic)
MVC
Model JS Objects
View DOM
Controller JS Classes
MVC
<html ng-app>
<head>
<script src='angular.js'></script>
<script src='controllers.js'></script>
</head>
<body ng-controller='UserController'>
<div>Hi {{user.name}}</div>
</body>
</html>
function XXXX($scope) {
$scope.user = { name:'Larry' };
}
Expressions
{{ 1 + 1 }}
{{ 946757880 | date }}
{{ user.name }}
<tabs>
<tab title='Active tab' view='...'>
<tab title='Inactive tab' view='...'>
</tabs>
<tooltip content='messages.tip1'>
Adding Controllers
angular.module('F1FeederApp.controllers', []).
The $scope variable –
controller('driversController', function($scope) {
Link your controllers and
$scope.driversList = [ view
{
Driver: {
givenName: 'Sebastian',
familyName: 'Vettel'
},
points: 322,
nationality: "German",
Constructors: [
},
Driver: {
givenName: 'Fernando',
App.js
angular.module('F1FeederApp', [
'F1FeederApp.controllers'
]);
<table>
<thead>
</thead>
<tbody>
<td>{{$index + 1}}</td>
<td>
{{driver.Driver.givenName}} {{driver.Driver.familyName}}
</td>
<td>{{driver.Constructors[0].name}}</td>
<td>{{driver.points}}</td>
</tr>
</tbody>
Loading data from the
server(services.js)
angular.module('F1FeederApp.services', []). $http - a layer on top
of XMLHttpRequest or JSONP
factory('ergastAPIservice', function($http) {
return $http({
method: 'JSONP',
url:
'http://ergast.com/api/f1/2013/driverStandin
gs.json?callback=JSON_CALLBACK'
});
}
return ergastAPI;
}); we create a new module
(F1FeederApp.services) and register a
service within that module
(ergastAPIservice).
Modified controller.js
angular.module('F1FeederApp.controllers', []).
controller('driversController', function($scope,
ergastAPIservice) {
$scope.nameFilter = null;
$scope.driversList = [];
ergastAPIservice.getDrivers().success(function (response) {
//Dig into the responde to get the relevant data
$scope.driversList =
response.MRData.StandingsTable.StandingsLists[0].DriverSt
andings;
});
});
Routes
Modified app.js
angular.module('F1FeederApp', [
'F1FeederApp.services',
'F1FeederApp.controllers',
'ngRoute'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when("/drivers", {templateUrl: "partials/drivers.html", controller: "driversController"}).
when("/drivers/:id", {templateUrl: "partials/driver.html", controller: "driverController"}).
otherwise({redirectTo: '/drivers'});
}]);
Partial views
<!DOCTYPE html>
<html>
<head>
<title>F-1 Feeder</title>
</head>
<body ng-app="F1FeederApp">
<ng-view></ng-view>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
</body>
</html>
Advanced AngularJS
Concept
Dependency Injection
Modularity
Digesting
Scope
Handling SEO
End to End Testing
Promises
Localization
Filters