@rob_rich
by Rob Richardson
November 15, 2013
Rob Richardson is principal of Richardson & Sons, LLC, builder of ASP.NET and Node solutions for small- to medium-sized businesses. Rob has a BFA and MSCIS, and has been building software in .NET and HTML for over a dozen years, and frequently enjoys sharing his passion for software development at user groups and community events. Follow Rob at @rob_rich or on his blog at http://robrich.org/.
What if web technology kept up with the web?
What if web programming was fun?
Data binding
Web parts
Your name:
Hello {{hello_name}}!
http://docs-angularjs-org-dev.appspot.com/cookbook/helloworld
<html ng-app>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="HelloCntl">
Your name: <input type="text" ng-model="name" />
Hello {{name}}!
</div>
</body>
</html>
function HelloCntl($scope) {
$scope.name = 'World';
}
Angular is an MVC framework for building
Single Page Applications (SPA)
Angular is a framework for building
thick clients in a browser
<script src="angular.js"></script>
Use Google's CDN if possible
<html ng-app>
Angular app from this dom node inward
Only one per web page
Typically put on <html> but it could be on any node
<input ng-model="name" />
{{name}}
ng-model data binds to input control's value
{{...}} binds to markup
Don't need to declare the view model first,
just bind to it and Angular will do the right thing.
HTML:
<div ng-controller="TheController">
JS:
function TheController($scope) {
$scope.name = 'World';
}
Controller for each view (widget)
Multiple controllers allowed on the page
$scope is the view model -- the data binding context
$scope is the single source of truth
not the DOM
No left-right data synchronization code
Yay!
HTML:
<li ng-repeat="person in people | orderBy:'name'">
{{person.name}}: age {{person.age}}
</li>
JS:
function TheController($scope) {
$scope.people = [
{name:'John',age:20},
{name:'Paul',age:23},
{name:'George',age:28},
{name:'Ringo',age:30}
];
}
Iterate through an array
Optionally sort
ng-repeat is on element to duplicate,
not parent node
HTML:
<a href="#" ng-click="doWork()" />
JS:
function TheController($scope) {
$scope.doWork = function () {
$scope.someval = 'new val';
};
}
bind a link or button to a controller function
HTML:
<div ng-show="isActive">shown if active is true</div>
JS:
function TheController($scope) {
$scope.isActive = true;
}
bind a link or button to a controller function
HTML:
<div ng-class="myVar">styled</div>
JS:
function TheController($scope) {
$scope.myVar = 'selected';
}
conditionally apply a CSS class
HTML:
<div ng-class="{selected: myVar===3}">styled</div>
JS:
function TheController($scope) {
$scope.myVar = 3;
}
conditionally apply a CSS class
JS:
function TheController($scope) {
$scope.$watch('name', function() {
$scope.greeting = $scope.salutation + ' ' + $scope.name + '!';
});
}
watch a value and run the function when it changes
JS:
function TheController($scope) {
$scope.$on('something', function(args) {
$scope.message = 'something happened';
});
// ...
$scope.$emit('something', {pass:'arguments'});
}
publish and subscribe to events
HTML:
<tag ng-app="someModule">
<tag ng-controller="SomeController">
JS:
angular.module('someModule', [])
.controller('SomeController', function ($scope) {
$scope.somemodel = "value";
});
a module is a container -- a namespace
JS:
angular.module('someModule', [])
.factory('someFactory', function () {
var serviceInstance = {
someMethod: function () {
// do something
}
};
return serviceInstance;
});
move business logic out of controllers
JS:
angular.module('someModule', ['otherModule'])
.factory('someFactory', function () { ... })
.controller('SomeController', function ($scope, someFactory) {
$scope.somemodel = "value";
});
Automatic Dependency Injection
In the box!
JS:
angular.module('themodule', ['some','dependencies'])
.directive('myRating', function () {
return {
restrict: 'EA', // element or attribute
templateUrl: '/path/to/file.html', // or template: '<some markup>'
scope: {
score: '=', // two-way binding between outer and inner scope
title: '@', // one-way binding into directive
val: '&' // one-way binding out of directive
},
controller: function($scope/*, $element, $attrs, $transclude*/) {
// stuff
}
};
});
HTML:
<my-rating score="score" title="the title" />
HTML:
<script src="#angular-route.js" />
JS:
angular.module('myModule', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'index.html',
controller: IndexController
})
.when('/book/:bookId', {
templateUrl: 'book.html',
controller: BookController
})
.otherwise({redirectTo:'/'});
});
HTML:
<a href="#/book/gatsby">click me</a>