On Github shizik / CodeCamp-MK-2013
Marko Ilievski Sr. Software Engineer, Netchexgithub.com/shizikgithub.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 :(
<label>Name:</label> <input type="text" id="name"> <p id="greeting"></p>
Hello !
<script type="text/javascript">
$(function () {
var $name = $('#name');
var $greeting = $('#greeting');
$name.keyup(function () {
$greeting.text('Hello '+ $name.val() + '!');
})
})
</script>
Lets see AngularJS?
<label>Name:</label>
<input type="text" ng-model="name">
<p>Hello {{name}}!</p>
Hello !
<input
ng-model="model.message"
value="Hello Campers!" />
$scope.model = {
message: "Hello Campers!"
};
$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>
<div>
Name: <input type="text" ng-model="name">
<p>The name is: <span ng-bind="name"></span></p>
</div>
<script src="Scripts/angular.js"></script>
</body>
</html>
<p>The name is: <span ng-bind="name"></span></p>
You should use handlebars instead of ng-bind
<p>The name is: {{ name }}</p>
And get much cleaner templates
<div ng-init="names=['Angular','Ember','Knockout']">
<p>Looping with the ng-repeat Directive</p>
<ul>
<li ng-repeat="name in names">{{ name }}</li>
</ul>
</div>
Looping with the ng-repeat Directive
Imagine teaching the HTML new tricks
Well you can! By using directives!
More on that later...
<div ng-controller="MyController">
Your name:
<input type="text" ng-model="username">
<button ng-click="sayHello()">greet</button>
<p>{{greeting}}</p>
</div>
<script type="text/javascript">
function MyController($scope) {
$scope.username = 'World';
$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}
</script>
<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
<ng-view></ng-view>
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 starthttp://docs.angularjs.org/guide/