On Github patrickarlt / dev-summit-talk-angular-js
Experience Developer - Esri Portland R&D Center
<!DOCTYPE html> <html> <head> <title>Mind Blown</title> <link rel="import" href="esri-maps.html"> </head> <body> <esri-map id="map"center="45.528, -122.680" zoom="15"> <esri-feature-layer id="parks" url="http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Parks_pdx/FeatureServer/0" <esri-feature-layer> </esri-map> <script type="text/javascript"> var map = document.getElementById("map"); map.on("click", function(e){ console.log("map was clicked on"); }); var parks = document.getElementById("parks"); parks.on("click", function(feature){ console.log("you clicked on " + feature.name); }); </script> </body> </html>
Angular is the only production-ready way to do ANYTHING like Web Components
Angular also has a TON of features for building web apps
~275 lines of code - http://mapattack.org
Angular has its own system for scoping and namespaceing modules.
var app = angular.module('todo', []);
<div ng-app="todo"> ... </div>
$scope represents our "Model" and how we interact with our data
Controllers bind our $scope to chunks of DOM
<div ng-app> <div ng-controller="MainCtrl"> {{title}} </div> </div>
function MainCtrl($scope){ $scope.title = "Hello World"; }http://jsfiddle.net/patrickarlt/qv2v5/
Expressions are simple code peices that Angular can parse and evaluate
Directives tell Angular HOW to bind $scope to the DOM
<div ng-app=""> <div ng-controller="MainCtrl"> <h1 ng-class="{big: isBig}">{{title}}</h1> </div> </div>
function MainCtrl($scope){ $scope.isBig = true; $scope.title = "Hello World" }http://jsfiddle.net/patrickarlt/5CRkr/1/
Bind data from $scope to DOM and vica-versa
<div ng-app=""> <div ng-controller="MainCtrl"> <input type="text" ng-model="title"> <h1>{{title}}</h1> </div> </div>
function MainCtrl($scope){ $scope.title = "Hello World" }http://jsfiddle.net/patrickarlt/w3ZU3/
Angular compares the new $scope the old $scope
If $scope changed, directives will handle changing the DOM
http://stackoverflow.com/questions/9682092/databinding-in-angularjs
First lets break Angular…
<div ng-app> <div ng-controller="MainCtrl"> <h1>{{title}}</h1> </div> </div>
function MainCtrl($scope){ $scope.title = "Hello World" setTimeout(function(){ $scope.title = "Angular is Magic!"; }, 500); }http://jsfiddle.net/patrickarlt/u5T9v/
Tells Angular you are changing the $scope.
<div ng-app> <div ng-controller="MainCtrl"> <h1>{{title}}</h1> </div> </div>
function MainCtrl($scope){ $scope.title = "Hello World" setTimeout(function(){ $scope.$apply(function(){ $scope.title = "Angular is Magic!"; }); }, 500); }http://jsfiddle.net/patrickarlt/JRvnH/
Most of the time you don't have to call $scope.$apply. Just use Angulars helpers.
function MainCtrl($scope, $timeout){ $scope.title = "Hello World" $timeout(function(){ $scope.title = "Angular is Magic!"; }, 500); }http://jsfiddle.net/patrickarlt/MzjL6/
var socket = io.connect('http://api.mapattack.org:8000'); socket.on('data', function(payload){ $scope.$apply(function(){ $scope.gameState = payload; }); });
Think of $scope.$apply as APPLYING changes to the SCOPE
Warning! Writing your own directives are a HUGE topic!
<div ng-controller="MapCtrl"> <esri-map id="map" center="-73.97163391113281,40.70579060224226" zoom="14" basemap="topo" onclick="click"> <esri-feature-layer url="http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/NYC_Police_Precincts/FeatureServer/0"></esri-feature-layer> <esri-feature-layer url="http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Graffiti_Locations3/FeatureServer/0"></esri-feature-layer> </esri-map> </div>
Working with JavaScript App Frameworks and ArcGIS API for JavaScript
4:00pm - 5:00pm - Today - Smoketree A-E
Backbone, Polymer, Web Components, Angular
Twitter : @patrickarlt
Slides : http://bit.ly/1cGPDLf