An introduction to the AngularJS JavaScript MVC framework from Google. Tailored for Java developers. Presented at the Orange County Java Users Group on 10/09/2014
2. <ng-selfie/>
Organizer of AngularJS-OC [angularjsoc.org, @angularjsoc]
Multi-platform SW geek => Java, Ruby, JavaScript, C#, Node
First an Ember fan (still am)
~1.5 years of AngularJS experience => mostly consulting
3. Agenda
Thick client history
Current state of JS
MVC & Data binding
Components
Tooling
Resources
11. The Model
Plain old Java(Script) objects POJO!
No need to extend a hierarchy
12. class Member {
String name = "";
boolean active = true;
List<String> emails = new List<String>();
}
var member = {
name: '',
active: true,
emails: []
};
13. The View
Just plain HTML
Built-in directives similar to JSTL
15. The Controller
Plain old JavaScript functions
Instantiated as needed
Inject dependencies
Figure out what the view needs, defer retrieval
$scope is the context
o a view model
16. package ocjug.controllers;
@Controller
class MeetupController {
function meetupController($scope) {
$scope.person = {
name: 'Loc', active: true, emails: [...]
};
}
public String index(Model model) {
Member person = new Member();
member.name = "Loc";
member.active = true;
member.emails = Arrays.asList(...);
model.setAttribute("scope", member);
}
}
angular.module('ocjug.controllers', [])
.controller('MeetupController', meetupController);
17. Data Binding
$digest loop Angular event loop
$watch list whats dirty?
http://codepen.io/anon/pen/EcoGd
18. Dependency Injection
Code to abstractions
Testing is so easy
SOLID
Put controllers on a diet
19. Services
Promote cleaner code
Organization and reusability
Shared business logic
Data retrieval
One instance in app
3 ways to make a service!
20. package ocjug.services;
@Service
class MeetupSearchService {
private final API_KEY = "abc123";
private final SEARCH_URI = "https://api.meetup.com/search";
@Autowired
SomeHttpClient httpClient;
public List<SearchResult> search(Map params) {
// start pseudo-ing
httpClient.queryParams(params).get(SEARCH_URI);
}
}
Example
21. angular.module('ocjug.services', [])
.factory('MeetupSearchSvc', function ($http) {
var API_KEY = 'abc123';
var SEARCH_URI = 'https://api.meetup.com/search';
var search = function (queryParams) {
return $http.get(SEARCH_URI, { params: queryParams
});
};
return {
search: search
}
});
22. Services
.service() - invoke with the new keyword
angular.module('ocjug.services', [])
.service('MeetupSearchService', function ($http) {
this.API = 'http://api.meetup.com/search';
this.search = function() {
// ...
}
});
23. Services (cont)
.factory() - always use a factory!
angular.module('ocjug.services', [])
.factory('MeetupSearchService', function ($http) {
var API = 'http://api.meetup.com/search';
return {
search: function (params) {
//
}
};
});
24. Services (cont)
.provider() - configure before app starts
angular.module('ocjug.services', [])
.provider('MeetupSearchProvider', function () {
var API = 'http://api.meetup.com/search';
this.REMEMBER_SEARCHES = false;
this.$get = function ($http) {
return {
search: function (params) {
//
if (this.REMEMBER_SEARCHES) ...
}
};
};
});
26. angular.module('ocjug.controllers', [])
.controller('MemberSearchCtrl', function ($scope, $http) {
$http.get('http://api.meetup.com/search?name=' +
$scope.name);
})
.controller('MeetupSearchCtrl', function ($scope, $http) {
$http.get('http://api.meetup.com/search?meetup='
+ $scope.meetup);
});
Extracting into a Service
27. var ocjug = angular.module('ocjug', ['ocjug.services']);
function memberSearchCtrl ($scope, MeetupSearchSvc) {
MeetupSearchSvc.search({ name: $scope.name });
}
ocjug.controller(MemberSearchCtrl, memberSearchCtrl);
function meetupSearchCtrl ($scope, MeetupSearchSvc) {
MeetupSearchSvc.search({ meetup: $scope.meetup });
}
ocjug.controller(MeetupSearchCtrl, meetupSearchCtrl);
28. Filters
Take an input to filter
Easily format data in templates
Uses the | character in {{ }} expression
{{1.456 | number:2}} => 1.46
{{'ocjug'| uppercase | limitTo:3}} => OCJ
{{99.99 | currency:'USD$' }} => USD$99.99
<div ng-repeat="m in movies | orderBy:'revenue'">