Andrey Kucherenko
Creator and curator of training platform for developers - https://startupemulator.com/. Math.random() community leader - https://t.me/mathrandomcommunity Engineer with more than 21 years of experience in IT.
Part 4
Author: Andrey Kucherenko
AngularJS: templates, directives, filters
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<title>JS+Angular Classes: Lesson 4</title>
<script src="lib/boundle.js" charset="utf-8"></script>
</head>
<body ng-controller="MyConroller">
<h1>JS+Angular Classes: Lesson 3</h1>
<p>{{'Hello ' + framework + 'JS!'}}</p>
</body>
</html>
angular.module('app', []).directive('myCustomer', function() {
return {
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
});
<div ng-controller="Controller">
<div my-customer></div>
<my-customer></my-customer>
</div>
angular.module('myApp').component('hello', {
templateUrl: 'hello.html',
controller: HelloController,
bindings: {
hero: '='
}
});
<!-- components match only elements -->
<div ng-controller="mainCtrl as ctrl">
<b>Hero</b><br>
<hello hero="ctrl.hero"></hello>
</div>
angular.module('myApp')
.controller('helloController', function ($scope) {
$scope.myModel = {
name: 'test'
}
});
var $compile = ...; // injected into your code
var scope = ...;
var parent = ...; // DOM element where the compiled template can be appended
var html = '<div ng-bind="exp"></div>';
// Step 1: parse HTML into DOM element
var template = angular.element(html);
// Step 2: compile the template
var linkFn = $compile(template);
// Step 3: link the compiled template with the scope.
var element = linkFn(scope);
// Step 4: Append to DOM (optional)
parent.appendChild(element);
{{ expression | filter }}
{{ expression | filter1 | filter2 | ... }}
{{ expression | filter:argument1:argument2:... }}
<div ng-controller="MyController">
<input ng-model="greeting" type="text"><br>
No filter: {{greeting}}<br>
Reverse: {{greeting|reverse}}<br>
Reverse + uppercase: {{greeting|reverse:true}}<br>
Reverse, filtered in controller: {{filteredGreeting}}<br>
</div>
var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', ($scope) => {
$scope.greeting = 'Hola JS!';
}]);
var MyController = function($scope, greeter) {
// ...
}
MyController.$inject = ['$scope', 'greeter'];
someModule.controller('MyController', MyController);
someModule.controller('MyController', function($scope, greeter) {
// ...
});
<div ng-app="myApp">
<div>
{{ 'World' | greet }}
</div>
</div>
var myAppModule = angular.module('myApp', []);
angular.module('finance2', [])
.factory('currencyConverter', function() {
//.. implementations
return {
methos: method,
data: data
};
});
<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="utf-8">
<title>JS+Angular Classes: Lesson 3</title>
<script src="lib/boundle.js" charset="utf-8"></script>
</head>
<body>
<h1>JS+Angular Classes: Lesson 3</h1>
<p>{{'Hello' + ' JS!'}}</p>
</body>
</html>
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8">
<title>JS+Angular Classes: Lesson 3</title>
<script src="lib/boundle.js" charset="utf-8"></script>
</head>
<body ng-controller="MyConroller">
<h1>JS+Angular Classes: Lesson 3</h1>
<p>{{'Hello ' + framework + 'JS!'}}</p>
</body>
</html>
import * as angular from 'angular';
angular.module('app', [
//.. list of dependencies
]).controller('MyConroller', ($scope) => {
$scope.framework = 'Angular';
console.log($scope);
});
<!DOCTYPE html>
<html ng-app="HolaApp">
<head>
<meta charset="utf-8">
<title>JS+Angular Classes: Lesson 4</title>
<script src="lib/boundle.js" charset="utf-8"></script>
</head>
<body>
<h1>JS+Angular Classes: Lesson 4</h1>
<div class="HolaApp">
<div ng-controller="MyController as ctrl">
<h2>Hola App</h2>
<p>{{'Hola ' + framework + 'JS!' + ctrl.fName}}!</p>
</div>
</div>
</body>
</html>
<button ng-click="count = count + 1" ng-init="count=0">
Increment
</button>
<span>
count: {{count}}
</span>
<button ng-click="clickHandler($event)">Test Click</button>
<button ng-controller="MyConroller as ctrl"
ng-click="ctrl.clickHandler($event)">
Test Click1
</button>
angular.module('app', [])
.controller('MyConroller', function ($scope) {
$scope.clickHandler = (event) => 'Angular';
this.clickHandler = (event) => 'Angular';
});
<span title="{{ attrBinding }}">{{ textBinding }}</span>
<p id="one-time-binding-example">
One time binding: {{::name}}
</p>
<input ng-model="foo">
<input type="text" name="userName"
ng-model="user.name"
ng-model-options="{ getterSetter: true }" />
<div ng-repeat="(key, value) in myObj"> ... </div>
<div ng-repeat="n in [42, 42, 43, 43] track by $index">
{{n}}
</div>
<header ng-repeat-start="item in items">
Header {{ item }}
</header>
<div class="body">
Body {{ item }}
</div>
<footer ng-repeat-end>
Footer {{ item }}
</footer>
<div class="slide-animate" ng-include="template.url"></div>
<ng-include
src="string"
[onload="string"]
[autoscroll="string"]>
...
</ng-include>
<label>
Click me:
<input
type="checkbox"
ng-model="checked"
ng-init="checked=true" />
</label><br/>
Show when checked:
<span ng-if="checked" class="animate-if">
This is removed when the checkbox is unchecked.
</span>
<!-- when $scope.myValue is truthy (element is hidden) -->
<div ng-hide="myValue" class="ng-hide"></div>
<!-- when $scope.myValue is falsy (element is visible) -->
<div ng-hide="myValue"></div>
<!-- when $scope.myValue is truthy (element is visible) -->
<div ng-show="myValue"></div>
<!-- when $scope.myValue is falsy (element is hidden) -->
<div ng-show="myValue" class="ng-hide"></div>
<div class="animate-switch-container"
ng-switch on="selection">
<div class="animate-switch"
ng-switch-when="settings">Settings Div</div>
<div class="animate-switch"
ng-switch-when="home">Home Span</div>
<div class="animate-switch"
ng-switch-default>default</div>
</div>
angular.module('app', []).directive('myName', function() {
return {
template: 'Name: {{name}}'
//templateUrl: 'path to html'
};
});
<div ng-controller="MyController">
<div my-name></div>
</div>
angular.module('app', []).directive('myName', function() {
return {
template: 'Name: {{name}}',
restrict: 'E' // matches element name
//restrict: 'A' // matches attrimbute name
//restrict: 'E' // matches class name
//restrict: 'M' // matches comment name
//restrict: 'AE' // matches element & attribue name
};
});
<div ng-controller="MyController">
<div my-name></div>
<my-name></my-name>
<div class="my-name: exp;"></div>
<!-- directive: my-directive exp -->
</div>
export function frameworkNameDirective() {
return {
scope: {
framework: '=info'
},
restrict: 'E',
template: 'Name: {{framework}}'
};
};
<framework-name info="framework"></framework-name>
export function ololoDirective() {
function link(scope, element, attrs) {
element.css({color: 'red'});
element.on('click', (event) => alert(event));
}
return {
link: link,
template: "<div>!{{name}}</div>"
};
};
export function ololoDirective() {
return {
restrict: 'E',
transclude: true,
scope: {},
controller: ['$scope', function($scope) {
var panes = $scope.panes = [];
$scope.select = function(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
};
this.addPane = function(pane) {
if (panes.length === 0) {
$scope.select(pane);
}
panes.push(pane);
};
}],
templateUrl: 'my-tabs.html'
};
};
function HeroDetailController() {
}
angular.module('app').component('heroDetail', {
templateUrl: 'heroDetail.html',
controller: HeroDetailController,
bindings: {
hero: '='
}
});
<!-- components match only elements -->
<div ng-controller="mainCtrl as ctrl">
<b>Hero</b><br>
<hero-detail hero="ctrl.hero"></hero-detail>
</div>
{{ filter_expression | filter : expression : comparator : anyPropertyKey}}
$filter('filter')(array, expression, comparator, anyPropertyKey)
<tr ng-repeat="friend in friends | filter:searchText">
<td>{{friend.phone}}</td>
</tr>
{{ currency_expression | currency : symbol : fractionSize}}
$filter('currency')(amount, symbol, fractionSize)
<script>
angular.module('currencyExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.amount = 1234.56;
}]);
</script>
<div ng-controller="ExampleController">
<input type="number" ng-model="amount" aria-label="amount"> <br>
default currency symbol ($): <span id="currency-default">{{amount | currency}}</span><br>
custom currency identifier (USD$): <span id="currency-custom">{{amount | currency:"USD$"}}</span>
no fractions (0): <span id="currency-no-fractions">{{amount | currency:"USD$":0}}</span>
</div>
{{ number_expression | number : fractionSize}}
$filter('number')(number, fractionSize)
angular.module('numberFilterExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.val = 1234.56789;
}]);
<div ng-controller="ExampleController">
<label>Enter number: <input ng-model='val'></label><br>
Default formatting: <span id='number-default'>{{val | number}}</span><br>
No fractions: <span>{{val | number:0}}</span><br>
Negative number: <span>{{-val | number:4}}</span>
</div>
{{ date_expression | date : format : timezone}}
$filter('date')(date, format, timezone)
<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>:
<span>{{1288323623006 | date:'medium'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>:
<span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'MM/dd/yyyy @ h:mma'}}</span>:
<span>{{'1288323623006' | date:'MM/dd/yyyy @ h:mma'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:"MM/dd/yyyy 'at' h:mma"}}</span>:
<span>{{'1288323623006' | date:"MM/dd/yyyy 'at' h:mma"}}</span><br>
By Andrey Kucherenko
Creator and curator of training platform for developers - https://startupemulator.com/. Math.random() community leader - https://t.me/mathrandomcommunity Engineer with more than 21 years of experience in IT.