Professional Documents
Culture Documents
3
And other angular tidbits
What is Angular?
AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets
you extend HTML's syntax to express your application's components clearly and succinctly. Angular's data binding and
dependency injection eliminate much of the code you would otherwise have to write. And it all happens within the
browser, making it an ideal partner with any server technology.
Angular is what HTML would have been had it been designed for applications. HTML is a great declarative language for
static documents. It does not contain much in the way of creating applications, and as a result building web
applications is an exercise in what do I have to do to trick the browser into doing what I want?
The impedance mismatch between dynamic applications and static documents is often solved with:
a library - a collection of functions which are useful when writing web apps. Your code is in charge and it calls into the library
when it sees fit. E.g., jQuery.
frameworks - a particular implementation of a web application, where your code fills in the details. The framework is in charge
and it calls into your code when it needs something app specific. E.g., durandal, ember, etc.
Angular takes another approach. It attempts to minimize the impedance mismatch between document centric HTML
and what an application needs by creating new HTML constructs. Angular teaches the browser new syntax through a
construct we call directives. Examples include:
Developed by Google
Started in 2009
Adoption is increasing
Angular Benefits
Is very modularized and gives you a great way to organize your javascript
code and break up work assignments to teams
Allows you to create your own elements and attributes using directives
Easily extensible
Routing
Embraces testing
Scope is an object that refers to the application model. It is an execution context for
expressions. Scopes are arranged in hierarchical structure which mimic the DOM
structure of the application. Scopes can watchexpressionsand propagate events.
Angular services are substitutable objects that are wired together using
dependency injection (DI). You can use services to organize and share code across your
app.
At a high level, directives are markers on a DOM element (such as an attribute, element
name, comment or CSS class) that tell AngularJS's HTML compiler ($compile) to attach a
specified behavior to that DOM element or even transform the DOM element and its
children including validation.
I build web business applications some intranet, some out in the wild.
Backend is EF6, Web Api and one Odata app just for learning purposes
For larger apps use BreezeJS (which I highly recommend) for client
communication with the server and its server side components.
Sample application
Technician Activity Tracking
Requirements be able to have a technician enter the orders they worked on,
the date worked, the hours for those orders, any issues encountered and any
materials used.
Backend Web Api and an Odata Controller just for fun, using Owin startup for
configuration, no global.asax
Client calls are all made using Angular resource module allowing interaction
with RESTful service
3 controllers, one for the links at the top of the page, one for adding
activities and one for viewing a grid of all activities
2 directives one for async validation of order number, one for validation of
hours worked
Validation
These directives detect validation errors on the ngModel object and allow
simpler communication of the validation issues
Can be validation from either the standard HTML5 attributes and input types
or custom directives including the ability to cross validate multiple controls.
The code
HTML Page
<div class="row col-md-7 ">
<label>Work Date:</label>
<div class="input-group">
</div>
<div ng-messages="activityForm.workDate.$error">
<div ng-message="required" class="alert alert-warning">Work Date is
required</div>
</div>
</div>
<input type="text" id="orderWorked" name="orderWorked" class="formcontrol" ng-model="activity.ordersWorked" required order-number-validator ng-modeloptions="{updateOn:'blur'}" ng-readonly="activityForm.orderWorked.$valid" />
</div>
<div ng-messages="activityForm.orderWorked.$error">
</div>
<div ng-if="activityForm.orderWorked.$pending">
Checking Order Number...
</div>
</div>
The directive
require: 'ngModel',
ngModel.$asyncValidators.validOrderNumber = function
(modelValue, viewValue) {
return datacontext.validateOrderNumber(val);
};
};
}]);
Comparative directive
div class="form-group">
<label class="control-label" for="testMe">Test Me:</label>
<input id="testMe" name="testMe" my-ui-validate="myTestFunction" ng-model-options="{updateOn:'blur'}"
ng-model="model.item1" class="form-control">
</div>
<div ng-messages="testForm.testMe.$error">
<div ng-message="validate">
Test string must be longer than four characters!
</div>
</div>
.directive('myUiValidate', [function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, elm, attrs, ngModel) {
var validateExpr = attrs.myUiValidate;
if (!validateExpr){ return;}
ngModel.$validators.validate = scope[validateExpr];
}
};
}])
$scope.myTestFunction = function(value){
return value.length > 4 ;
};