Passion: Better software through testing
"AngularJS is what HTML would have been, had it been designed for building web-apps"
"When you develop in Angular, you are developing in a better browser!"
"Pretty sure that HTML6 is going under the codename @angularjs!"
<input id="toggleShowHide" type="checkbox"> <div id=”specialParagraph”> This content will disappear and reappear if you click the checkbox above
</div>
<script>
$(function() {
function toggle() {var isChecked = $('#toggleShowHide').is(':checked');
var specialParagraph = $('#specialParagraph');if (isChecked) { specialParagraph.show(); }
else { specialParagraph.hide(); }
}
$('#toggleShowHide').change(function() {
toggle();
});
toggle();
});
</script>
<input ng-model="showSpecial" type="checkbox">
<div ng-show=”showSpecial”> This content will disappear and reappear if you click the checkbox above </div>
"Conceptually, Directives are NOT packaged JQuery"
<h1> {{ person.firstname }} <h1>
<h1 ng-bind="person.firstname > </h1>
<p> 1 + 1 = {{ 1 + 1 }} </p>
<ul>
<li ng-repeat="person in persons | orderBy: 'lastname'">
{{ person.firstname }}
, {{ person.lastname | uppercase }}
</li>
</ul>
/scripts/app.js
angular.module('railsApp', ['ngRoute', 'ngResource', 'ui-bootstrap']);
/scripts/controllers/main.js
angular.module('railsApp') .controller('MainCtrl', function($scope, $routeParam, Person){ $scope.person = Person.get($routeParams.personId);
$scope.save = function(person){ Person.save(person);
}
});
"A way to teach HTML new Tricks"
"Create your own HTML tags"
"Directive === Reusable Component"
"Register Behavior"
"Transform / Manipulate the DOM"
Element
<ng-view></ng-view>
Attribute
<div ng-view></div>
Class
<div class="ng-view"><div>
Sample
<tabset>
<tab ng-repeat="tab in tabs" heading="{{tab.title}}"
active="tab.active" disabled="tab.disabled">
{{tab.content}}
</tab>
</tabset>
angular.module('railsApp', ['ngAnimate']);
class PersonController < ApplicationController
def index
@persons = Person.get(params[:person])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @persons, root: false }
end
end
# other actions
end
angular.module('railsApp', ['ngResource'])
.factory('Person', function person($resource){
return $resource('/persons/:id', {id: '@id'}{
// DEFAULT IMPLEMENTATION OF $RESOURCE
// 'get': {method:'GET'}, // 'save': {method:'POST'}, // 'query': {method:'GET', isArray:true}, // 'remove': {method:'DELETE'}, // 'delete': {method:'DELETE'} }
}
angular.module('railsApp')
.controller('MainCtrl', function($scope, Person){
$scope.persons = Person.query();
$scope.add = function(newPerson){
Person.save(newPerson);
}
}
Application Level Module
angular.module('railsApp', []);
Module per feature
angular.module('railsApp', ['ngAnimate', 'ngRoute', 'ngResource']);
app.js
angular.module('railsApp', []);
index.html
<!DOCTYPE HTML> <html ng-app="railsApp"> <head>
<title>AngularJS with Rails</title>
</head>
<body>
<h1> {{ rails.awesomeTitle }} </h1>
<script src="scripts/vendor/angular/angular.min.js">
<script src="scripts/app.js"></script>
</body>
</html>
$scope.person = {
firstName: 'Jan'
lastName: 'Progger'
}
controller.js
<h1> {{ person.firstname }} </h1>
views/main.html
scripts/controllers/main.js
"Expose model objects and methods to the View "
angular.module('railsApp') .controller('MainCtrl', function ($scope, Person) { $scope.persons = Person.getPersons();
$scope.addPerson = function(newPerson){
Person.save(newPerson)
} });
views/main.html
<div ng-controller="MainCtrl as main">
<p> {{ main.person.firstname }} {{ main.person.firstname }} </p>
</div>
scripts/controllers/main.js
angular.module('railsApp') .controller('MainCtrl', function () {
this.person = { firstName: 'Jan' lastName: 'Progger' }
});
angular.module('railsApp', ['ngRoute']) .config(function ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl' })
.when('/person/:name', { templateUrl: 'views/person.html', controller: 'PersonCtrl' }) .otherwise({ redirectTo: '/' }); });
scripts/services/person.js
angular.module('railsApp').factory('Person', function(){
var personFactory = {};
var persons = {
'77': {
firstName: 'Jan',
lastName: 'Progger
}
}
personFactory.get = function(personId){
return persons[personId];
}
return personFactory;
});
scripts/services/constants.js
angular.module('railsApp').constant('COMPANY', 'Gino B.V.');
angular.module('railsApp').constant('THE_ANSWER', 42);
scripts/controllers/main.js
angular.module('railsApp').controller('MainCtrl',
function(COMPANY, THE_ANSWER){
console.log('Company is ' + COMPANY);
console.log('The all knowing answer is: ' + THE_ANSWER);
}
Test Runner: Karma testrunner
Unit Testing: Jasmine
End-2-End testing: Protractor
yo angular demo
------
yo angular:controller person
------
yo angular:route myRoute --coffee
------
yo angular:factory myFactory --minsafe
bower update
------
bower search angular
------
bower install angular --save
grunt test
----------
grunt build
----------
grunt server