@rob_rich
#nodeaz
#angularjs
by Rob Richardson
September 16, 2013
Rob Richardson is principal of Richardson & Sons, LLC, builder of ASP.NET and Node solutions for small- to medium-sized businesses. Rob has a BFA and MSCIS, and has been building software in .NET and HTML for over a dozen years, and frequently enjoys sharing his passion for software development at user groups and community events. Follow Rob at @rob_rich or on his blog at http://robrich.org/.
What if web technology kept up with the web?
What if web programming was fun?
Data binding
Web parts
Your name:
Hello {{hello_name}}!
http://docs-angularjs-org-dev.appspot.com/cookbook/helloworld
<html ng-app>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="HelloCntl">
Your name: <input type="text" ng-model="name" value="World"/>
Hello {{name}}!
</div>
</body>
</html>
function HelloCntl($scope) {
$scope.name = 'World';
}
ng-*
{{markup}}
$scope.property
... is live data binding,
you can fall asleep now
What I want:
<rating data-bind="score"></rating>
What I get:
<div id="better-be-unique">
<img src="this/gets/redundant.png" />
<img src="this/gets/redundant.png" />
<img src="this/gets/redundant.png" />
<img src="this/gets/redundant.png" />
<img src="this/gets/redundant.png" />
<script>
function polute_the_global_scope(e) {
if (e.target === 'one') {
return 1;
} else if (e.target === 'two') {
return 2;
} else if (e.target === 'redundant') {
return 3;
} else ...
}
</script>
</div>
HTML:
<div id="better-be-unique">
<img src="star_{{isOn(1)}}.png" ng-click="setScore(1)" />
<img src="star_{{isOn(2)}}.png" ng-click="setScore(2)" />
<img src="star_{{isOn(3)}}.png" ng-click="setScore(3)" />
<img src="star_{{isOn(4)}}.png" ng-click="setScore(4)" />
<img src="star_{{isOn(5)}}.png" ng-click="setScore(5)" />
</div>
JavaScript:
angular.module('themodule', [])
.directive('rating', function () {
return {
restrict: 'EA',
templateUrl: '/path/to/rating.html',
scope: {
score: "="
},
controller: function($scope/*, $element, $attrs, $transclude*/) {
$scope.click = function (val) {
$scope.score = val;
}
$scope.isOn = function (val) {
return (val <= $scope.score) ? 'on' : 'off';
}
}
};
});
What I get:
<rating score="score"></rating>
<script src="rating.js"></script>
SOLVED!
A framework for the modern web