AngularJS
-
Upload
luka-zakrajsek -
Category
Technology
-
view
3.532 -
download
3
description
Transcript of AngularJS
ANGULAR'S PHILOSOPHYDecouple DOM manipulation from app logic
Code reusage
Make common tasks trivial anddifficult tasks possible
Not a library
MODULESSplit your application into multiple files
No global namespace manipulation
Doesn't limit you with files structure
$SCOPEOne $rootScope
Every controller gets its own scopeand inherits data from parent controller
Every directive gets new $scopethat is isolated from others
URL ROUTING.config(['$routeProvider', function($routeProvider) { $routeProvider.when('/home', { templateUrl: 'partials/home.html', controller: 'HomeController' });
$routeProvider.when('/comments', { templateUrl: 'partials/comments.html', controller: 'CommentsController' });
$routeProvider.otherwise({ redirectTo: '/home' });}]);
CONTROLLERS.controller('CommentsController', function($scope, Comment) { Comment.query(function(res) { res.reverse(); $scope.comments = res; });
$scope.post = function(){ $scope.comments.unshift({ name: $scope.user.name, email: $scope.user.email, content: $scope.comment });
$scope.comment = ''; }})
FILTERS.filter('gravatar', function() { return function(email, size) { var hash = $().crypt({ method: 'md5', source: email });
return "http://secure.gravatar.com/avatar/" + hash + ".jpg?s=" + size + "&d=identicon"; };})
DIRECTIVES.directive('hasFocus', function($timeout) { return function(scope, elm, attrs) { scope.$watch(attrs.hasFocus, function(value) { if (value) { $timeout(function(){ elm.focus(); }, 0); } }); };})
DIRECTIVES.directive('debug', function() { return { restrict: 'E', scope: { data: '=', title: '@', }, templateUrl: 'partials/debug.html', controller: function($scope, $element) {
} };})
DIRECTIVESdebug.html
Usage:
<h4>{{ title }}:</h4>
<pre>{{ data | cleanData | json }}</pre>
<debug data="user" title="User"></debug>
TESTINGUnit testingEnd to end testing
it('should reverse greeting', function() { expect(binding('greeting|reverse')).toEqual('olleh'); input('greeting').enter('ABC'); expect(binding('greeting|reverse')).toEqual('CBA');});
DEMOSample application
Some code
Chrome plugin
Code available on Github:https://github.com/bancek/angularjs-talk