intro to angularjs
TRANSCRIPT
WHOAMI
Tom Wilson● Lead Technologist JRS● <3 JavaScript● <3 io.js/node● <3 couchdb/pouchdb● <3 angularjs/mercury
What is AngularJS?
AngularJS is a JavaScript Framework that abstracts away the direct manipulation of the DOM using two-way data-binding and leverages html templates and dependency injection to create applications.
Why AngularJS
● Simpler code● Easier manage DRY Code● Easier to Unit Test● Flexible Organization● Easy to integrate with other JS Libs
Getting Started
Install
● Download - https://angularjs.org/● bower install angular● npm install angular
Two-Way Databinding
<body ng-app> <h1>{{title}}</h1> <input type=”text” ng-model=”title”></body>
http://jsbin.com/verifa/1/edit?html,output
Template Example<body ng-app=”App”>
<section ng-controller=”MainCtrl”>
<h2>{{appTitle}}</h2>
<article ng-repeat=”article in articles”>
<header>
<h2>{{article.title}}</h2>
</header>
{{article.body}}
</article>
</section>
</body>
Built-In Directives
Directives are custom elements, attributes, or class names that enables declarative coding.
<body ng-app=”myApp”></body>
ng-repeat<body ng-app>
<div ng-init=”list = [1,2,3,4]”></div>
<ul>
<li ng-repeat=”n in list”>{{n}}</li>
</ul>
</body>
http://jsbin.com/sisuku/1/edit?html,output
Controllers<body ng-app=”app”>
<div ng-controller=”FooCtrl”>
<h1>{{color}}</h1>
</div>
</body>
angular.module('app', [])
.controller('FooCtrl', function ($scope) {
$scope.color = 'green';
});
http://jsbin.com/qejime/3/edit?html,js
Routinghttp://jsbin.com/tanoko/3/edit?output
UI-Routerhttp://angular-ui.github.io/ui-router/site/#/api/ui.router
Configuration - $stateProvider
angular.module(‘app’, []) .config(function($stateProvider, $urlRouterProvider) { $stateProvider .state(‘foo’, { url: ‘/foo’, controller: …, template: …, })
Configuration - $urlRouterProvider
angular.module(‘app’, []) .config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider
.otherwise(‘/’) … })
Viewshttp://jsbin.com/koreki/2/edit?html,output
Serviceshttp://jsbin.com/yakuti/3/edit?js,output
Custom Directiveshttp://jsbin.com/yuhulu/4/edit?html,js
Unit Testing
// ng-mock
describe(‘foo’, function() { beforeEach(inject(...)) it(‘bar’, function() { expect(bam).toEqual(‘bam’) })})
Resourceshttps://github.com/jmcunningham/AngularJS-Learning
Multiple Modules
angular.module(‘foo’, []) .constant(‘bar’, ‘baz’)
var app = angular.module(‘app’, [‘foo’]) .run(function(bar) { console.log(bar) })
Models
<body ng-app> <div ng-init=”color = ‘red’”></div> <h1>{{color}}</h1> <input type=”text” ng-model=”color”></body>
http://jsbin.com/geqede/1/edit?html,output
Filtershttp://jsbin.com/qavixu/1/edit?html,output
Filters - Collectionshttp://jsbin.com/comisot/1/edit?html,js,output
ng-classhttp://jsbin.com/yuhulu/1/edit?html,output
ng-clickhttp://jsbin.com/zagoxa/2/edit?html,js,output
Formshttp://jsbin.com/naziheduje/1/edit?html,js,output
$http
Is a service to make ajax calls in AngularJS
.controller(‘fooCtrl’, function($scope, $http) {
$http.get(‘/someurl’)
.then(function(res) {
$scope.widgets = res
})
})