Angularjs: HTML Enhanced For Web Apps!
Angularjs: HTML Enhanced For Web Apps!
<p ng-show="isShown">Yes!</p>
MVC
<button ng-click='recompute()'>=</button>
Controller
Controller is defined at root scope.
function HelloController($scope){
$scope.target = 'world'
}
Directives
<ul ng-controller='ListController'>
<li ng-repeat='fruit in fruits'>
{{fruit.name}} = {{fruit.amount}}
</li>
</ul>
Script
{{7/3 | number}}
{{7/3 | number:1}}
{{12000 | currency:'THB'}}
... ng-repeat="f in fruits | orderBy:'amount'" ...
Modules
Useful Features
function ListController($scope){
$scope.fruits = [{name: 'mango', amount: 2},
{name: 'banana', amount: 5},
{name: 'orange', amount: 4}];
}
<form name="addFruitForm">
<div>Name: <input ng-model='fruit.name' required
ng-minlength='3'></div>
<div>
Amount: <input type='number' min='1' max='10'
ng-model='fruit.amount' name='amount' required>
<span class="label label-warning"
ng-show='!addFruitForm.amount.$valid'>
1-10
</span>
</div>
<div><button ng-disabled='!addFruitForm.$valid'
ng-click='addFruit()'>Add</button></div>
</form>
Filters
Script
Module Loading
Modules can list other modules as their dependencies.
Note: include ui-bootstrap.js and bootstrap.css
Template
<div>
Focus me: <input tooltip-trigger="focus"
tooltip-placement="right" tooltip="See?"/>
</div>
Services
There are three functions for creating generic services, with
different levels of complexity and ability.
[provider | factory | service]
myApp.factory('Book', function(){
var book = {};
book.query = function(){
return [
{title: 'AngularJS', price: 500},
{title: 'CSS', price: 300},
{title: 'Bootstrap', price: 320}
];
};
return book;
});
myApp.controller('BookCtrl', function($scope, Book){
$scope.books = Book.query();
});
MEAN Stacks
MongoDB, Express, Angular, Node
{
"name": "angularjs-seed",
"description": "A starter project for AngularJS",
"dependencies": {
"express": "3.x"
},
"devDependencies": { ... }
{method:'GET'},
{method:'POST'},
{method:'GET', isArray:true},
{method:'DELETE'},
{method:'DELETE'} };
Promise
An interface for interacting with an object that represents
the result of an action that is performed asynchronously,
and may or may not be finished at any given point in time.
Note: you can chain then.
promise.then(function(greeting) {
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
Unit Testing
With great power comes great responsibility.
Karma
Install karma globally so we can run unit-testing from
command line.
>> npm install g karma
Create karma configuration files by running this command
at project directories.
>> karma init
Bootstrap
describe('unit-test', function() {
it('should work', function(){
expect(true).toBe(true);
});
});
Accordion
Routes
Though AJAX apps are technically single-page apps (in the
sense that they only load an HTML page on the first request,
and then just update areas within the DOM thereafter), we
usually have multiple sub-page views.
Note: include angular-route.js in your code.
Template
<body>
<div ng-view>
</div>
</body>
<script type="text/javascript">
var spa = angular.module('spa', ['ngRoute']);
spa.config(function($routeProvider) {
$routeProvider.
when('/', {
controller: 'WelcomeController',
templateUrl: 'spa/welcome.html'}).
when('/hello/:id', {
controller: 'HelloController',
templateUrl: 'spa/hello.html'}).
otherwise({redirecTo: '/'});
});
... controllers ...
</script>
Navigation
Change hash fragment to navigate.
<!-- welcome.html -->
<a ng-href='#/hello/{{person.id}}'>
{{person.name}}
</a>
<!-- hello.html -->
<a ng-href="#/">Back</a>
Alert
Directive to generate alerts from the dynamic model data.
Template
<alert ng-repeat="alert in alerts" type="alert.type"
close="closeAlert($index)">
{{alert.msg}}
</alert>
<form class="form-inline">
<div class="form-group">
<input class="form-control" type="text"
ng-model="message">
</div>
<button class="btn" ng-click="addAlert()">
Click
</button>
</form>
Script
$scope.message = "";
$scope.addAlert = function(){
$scope.alerts.push({msg: $scope.message});
$scope.message = "";
}
$scope.closeAlert = function(index){
$scope.alerts.splice(index, 1);
}
Carousel
Use a <carousel> element with <slide> element inside it. It
will automatically cycle through the slides.
Template
<carousel interval="myInterval">
<slide ng-repeat="slide in slides"
active="slide.active">
<img ng-src="{{slide.image}}"
style="margin:auto;">
<div class="carousel-caption">
<h4>Slide {{$index}}</h4>
<p>{{slide.text}}</p>
</div>
</slide>
</carousel>
PUPA & CNR, COE/PSU - V1.08
Script
$scope.myInterval = 5000;
$scope.slides = [
{image: 'cat1.jpg', text: 'Cat1'},
{image: 'cat2.jpg', text: 'Cat2'},
{image: 'cat3.jpg', text: 'Cat3'},
];
Date Picker
Everything is formatted using the date filter and thus is also
localized.
Note: include angular-locale-th.js for Thai date format.
Template
<h4>Popup</h4>
<pre>Selected date is:
<em>{{dt | date:'fullDate' }}</em>
</pre>
<input type="text"
class="form-control" ng-model="dt"
datepicker-popup="{{format}}" />
Script
$scope.today = function() {
$scope.dt = new Date();
};
$scope.today();
$scope.format = 'dd-MMMM-yyyy';
Time Picker
Settings can be provided as attributes in the <timepicker>
or globally configured through the timepickerConfig
Template
<pre>Time is: {{mytime | date:'shortTime'}}</pre>
<div ng-model="mytime" style="display: inline-block">
<timepicker hour-step="hstep"
minute-step="mstep"
show-meridian="ismeridian">
</timepicker>
</div>
</div>
Script
$scope.mytime = new Date();
$scope.hstep = 1;
$scope.mstep = 15;
$scope.ismeridian = true;
Tab
Use <tab> directive in <tabset> directive.
Template
<tabset>
<tab ng-repeat="tab in tabs"
heading="{{tab.title}}"
active="tab.active">
{{tab.content}}
</tab>
</tabset>
Script
$scope.tabs = [
{ title:"Title 1", content:"Content 1" },
{ title:"Title 2", content:"Content 2",
disabled: true }
];
Modal
Create a partial view, its controller and reference them.
Template
$scope.ok = function () {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});