Marko Ilievski
Sr. Software Engineer, Netchex
github.com/shizik
github.com/wcpro/ScaffR
What is a SPA anyway?
Web app providing
rich and fluid UX
by loading and executing code
within a single page
What's wrong with jQuery?
It's imperative :(
Hello {{ example1 }}!
<script type="text/javascript">
$(function () {
var $name = $('#name');
var $greeting = $('#greeting');
$name.keyup(function () {
$greeting.text('Hello '+ $name.val() + '!');
})
})
</script>
Lets see AngularJS?
Hello {{name}}!
Hello {{ example2 }}!
<input ng-model="model.message" value="{{ message }}" />
$scope.model = { message: "{{ message }}" };
$scope.model.message = "Hello there!"
Render
↓
Render
↓
Render
↓
Render
↓
Render
↓
$apply
There are other frameworks, why Angular?!?
Well...
“Angular is the only framework that doesn’t make MVC seem like putting lipstick on a pig.”
<!DOCTYPE html>
<html ng-app>
<head>
<title></title>
</head>
<body>
Name:
The name is:
<script src="Scripts/angular.js"></script>
</body>
</html>
The name is:
You should use handlebars instead of ng-bind
The name is: {{ name }}
And get much cleaner templates
Looping with the ng-repeat Directive
- {{ name }}
Looping with the ng-repeat Directive
Imagine teaching the HTML new tricks
Well you can! By using directives!
More on that later...
Your name:
{{greeting}}
$scope
→ Controller
<script type="text/javascript">
function MyController($scope) {
$scope.username = 'World';
$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}
</script>
{{greeting}}
<html ng-app="moduleName">
var myModule = angular.module('myModule', []);
// add some controllers and services
myModule.controller('myController', ...);
myModule.service('myService', ...);
// Add provider configuration (route provider for example)
myModule.config(function(){...});
// Add service configuration
myModule.run(function(){...});
var myModule = angular.module('myModule', ['ngRoute']);
myModule.config(function ($routeProvider) {
$routeProvider
.when('/',
{
controller: 'indexController',
templateUrl:'index.html'
})
.when('/details',
{
controller: 'detailsController',
templateUrl:'details.html'
})
.otherwise({ redirectTo: '/' });
});
<div ng-view></div>
or
var myModule = angular.module('myModule');
myModule.factory('customerService', function() {
var newServiceInstance = {};
var customers = [ ... ];
newServiceInstance.getCustomers = function () {
return customers;
};
return newServiceInstance;
});
var myModule = angular.module('myModule');
myModule.controller('customerCtrl', function($scope, customerService) {
$scope.customerList = customerService.getCustomers();
// do stuff with the list
});
Putting it all together
Be careful when using $apply
// Bad
$scope.message = "Yo!";
$scope.$apply();
//Good
$scope.$apply(function() {
$scope.message = "Yo!";
});
Tools
Batarang | Yeoman | Karma
UI Components
AngularUI | KendoUI | Wijmo
Libraries
BreezeJS | Firebase
Good place to start
http://docs.angularjs.org/guide/