AngularJS Fundamentals BCC 2016
AngularJS Fundamentals BCC 2016
Fundamentals
by Jeremy Zerr
Blog: http://www.jeremyzerr.com
LinkedIn: http://www.linkedin.com/in/jrzerr
Twitter: http://www.twitter.com/jrzerr
What is AngularJS
Web Apps
ng-controller provides an
identifier to link with code
Controller is ToddlerCtrl
Data sources:
Directives + AngularJS templating allows you to create custom HTML markup, both elements
and attributes
Templating types:
We’ll take our existing code, pick the local JSON file as the data source, and show a
comparison between these different templating methods.
You can also use a $scope variable to control which CSS class
gets applied to an element
If the input changes its value, the underlying $scope variable also
changes.
How would you have done this with jQuery + Mustache? See this
Plunker
Two-way data binding:
ng-model + ng-class
The ng-class construct also applies the appropriate class as the underlying
model changes
You can also do read-only data binding using ng-bind instead of ng-model
Filters and Formatters
You can use $watch on a $scope variable to hook into whenever it is changed
Client-side routing
As seen in other examples, the syntax makes the Asynchronous REST API
call “appear” synchronous.
The call to query() returns an empty object, that is filled back in when the
AJAX response returns.
Other than $watch, you have a lot of other hooks into view changes. Here we use ng-change to call a
function to issue an AJAX $save to the REST API
The argument to the Adult REST API is pulled from the $routeParams, the server is called like /adults/1.json
How Does $watch Work
It goes along with $digest and $apply, it is a part of the Digest cycle.
When an $apply is run, this causes the Digest cycle to kick off and
compare the variable values, previous to current. This only happens
to variables you have bound to.
This part of AngularJS is likely where you are first exposed to it’s
inner workings. Why? Custom Directives!
Custom Directives
Let’s build a mini-app
X-Files Villains App - Demo
X-Files is Baaaaaacck!!!!
Plunker Link
X-Files Villains App
Choices to consider
Ways to reference a
directive from within
a template.
Equivalent examples
of an attribute that
would match ng-
bind.
What I haven’t (and won’t)
cover in detail
Form validation
Instead of ngResource, you can just use $http for lower level
control (closer to jQuery ajax/get)
Animations
Lots more…
Why Use AngularJS?
Now we know what AngularJS can do.
Why should we integrate it into our web application?
Why should you use AngularJS
in your next web app?
Client-side templating
Putting functionality embedded in HTML may not feel like good enough
separation of presentation and code.
Using PhantomJS to create snapshots and save, then use #! in URL: link
React by itself is not a fair comparison with AngularJS. React is typically paired with
something like Flux or Redux to be able to do full apps.
AngularJS is all about 2 way data binding. React approach is about managing state and
one way data flow.
AngularJS uses watches and data binding to know when to update DOM. React uses a
Virtual DOM to improve DOM updating efficiency by only applying changes.
React encourages using immutable data structures, resulting in operations one way.
AngularJS uses its own templating system, React can use JSX templates that exist right
alongside code.
AngularJS has a lot more contributed libraries and UI components because it has been
around longer.
Angular 1.5 vs. Angular 2
Angular 2 apps will be built entirely of components, like React. Directives in 1.x are the
closest thing we have to what a component looks like.
Angular 1.x release are adding small steps you can take to make the upgrade easier.
Angular 1.5 even added a component type, that is basically a more restrictive directive.
Will be able to use server side templating, able to be used easier in all types of apps in
more Javascript environments.
Most examples are in TypeScript, but will also be able to use ES2015.
Just like React, encourages using explicit data operations, resulting in operations one
way and in general improving app performance over Angular 1.
Frontend Web Dev Trends
Frontend separate from backend, using API for communication. Enables web
apps, and mobile apps, to be developed without rethinking architecture.
Ready to scale and allows developer specialization.
See all the code from this presentation, and more, at My Plunker Page
Blog: http://www.jeremyzerr.com
LinkedIn: http://www.linkedin.com/in/jrzerr
Twitter: http://www.twitter.com/jrzerr