"Angular is what the browser would have been like if it were designed for AJAX-style web apps. We teach the browser new tricks."
— Miško Hevery, creator of AngularJS
<p>Hello World!</p>)jQuery is a great tool...
... but it has limitations
Modules to the rescue!
Defining a module
// angular.module(name, [dependencies])
angular.module('myApp', ['ngRoute'])
Fetching a module from DIC
// angular.module(name)
angular.module('myApp')
Content Tab 1
Content Tab 2
Content Tab 3
There was an error!
don't know, never used before
Formatters for certain data
{{ date | date:'dd/MM/yyyy' }}
{{ amount | currency }}
{{ listOfObjects | filter:'filterString' | orderBy:'objectProperty' }}
Any shared code
angular
.module('myApp', [])
.factory('MD5', function() {
return function(input) {
// MD5 implementation
};
})
.directive('gravatar', function (MD5) {
return {
restrict: 'E',
template: '
',
scope: {
email: '@'
// ... Other scope properties
},
link: function (scope, element, attrs) {
// rest of implementation
}
};
});
Shared values
angular
.module('myApp', [])
.value('value', {
key: 'MyValue'
})
.controller('Controller1', function ($scope, value) {
$scope.setValue = function (newValue) {
value = newValue;
};
})
.controller('Controller2', function ($scope, value) {
$scope.value = value;
});
Shared constants
angular
.module('myApp', [])
.value('PI', Math.PI)
.controller('Controller1', function ($scope, PI) {
$scope.setValue = function (newValue) {
PI = newValue; // This assignment will not be persisted
};
})
.controller('Controller2', function ($scope, PI) {
$scope.pi = PI;
});
/*
* Confguration
*/
angular
.module('myModule', [])
.config(function($routeProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'views/home.html'
})
.when('/items/:id', {
controller: 'ItemsController',
templateUrl: 'views/item.html'
})
.otherwise({
redirectTo: '/'
});
});
<body>
<div ng-view></div>
</body>
ng-valid, ng-invalid, ng-pristine, ng-dirty$valid, $invalid, $pristine, $dirty, All modern browsers
Desktop and mobile
IE8 and earlier require some additional work
IE Compatibility Guidelines
With non-minified JavaScript
// function (injectables...)
function ($scope, $http) { /* ... */ }
With minified JavaScript
// [injectables..., function(injectables...)]
['$scope', '$http', function(s, h) { /* ... */ }]