Skip to content

Commit b1f7153

Browse files
committed
angular-js example cleanup
1 parent 2710d36 commit b1f7153

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

angular-js-example.html

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>Angular Timer Directive - Simple AngularJS Example</title>
5-
4+
<title>Timer Directive used from AngularJS</title>
65
<script src="angular/angular.min.js"></script>
76
<script src="app/js/timer.js"></script>
87
<script>
98
angular.module('MyApp', ['timer']);
9+
function MyAppController($scope) {
10+
$scope.timerRunning = true;
11+
12+
$scope.startTimer = function (){
13+
$scope.$broadcast('timer-start');
14+
$scope.timerRunning = true;
15+
};
16+
17+
$scope.stopTimer = function (){
18+
$scope.$broadcast('timer-stop');
19+
$scope.timerRunning = false;
20+
};
21+
}
22+
MyAppController.$inject = ['$scope'];
1023
</script>
1124
</head>
1225
<body ng-app="MyApp">
13-
<div>
14-
<timer id="timer1"></timer>
15-
</div>
16-
<div>
17-
<timer interval="5000" id="timer2"></timer>
26+
<div ng-controller="MyAppController" style="border: 1px solid gainsboro; text-align: center;">
27+
<h2>Single Timer</h2>
28+
<h3><timer/></h3>
29+
<button ng-click="startTimer()" ng-disabled="timerRunning">Start Timer</button>
30+
<button ng-click="stopTimer()" ng-disabled="!timerRunning">Stop Timer</button>
1831
</div>
19-
<div>
20-
<timer id="timer3">
21-
{{minutes}} minutes, {{seconds}} seconds.
22-
</timer>
32+
<br/>
33+
<div ng-controller="MyAppController" style="border: 1px solid gainsboro; text-align: center;">
34+
<h2>Multiple Timers</h2>
35+
<h3>Timer 1: <timer interval="2000"/></h3>
36+
<h3>Timer 2: <timer>{{minutes}} minutes, {{seconds}} seconds.</timer></h3>
37+
<button ng-click="startTimer()" ng-disabled="timerRunning">Start Timers</button>
38+
<button ng-click="stopTimer()" ng-disabled="!timerRunning">Stop Timers</button>
2339
</div>
2440
</body>
2541
</html>

app/js/timer.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@ angular.module('timer', [])
66
scope: {interval: '=interval'},
77
controller: function ($scope, $element) {
88
if ($element.html().trim().length === 0) {
9-
$element.append($compile('<h3>{{timeTaken}}</h3>')($scope));
9+
$element.append($compile('<span>{{timeTaken}}</span>')($scope));
1010
}
1111

1212
$scope.startTime = null;
1313
$scope.timeoutId = null;
1414

15+
$scope.$on('timer-start', function (){
16+
$scope.start();
17+
});
18+
19+
$scope.$on('timer-stop', function (){
20+
$scope.stop();
21+
});
22+
1523
$scope.start = $element[0].start = function () {
1624
$scope.startTime = new Date();
1725
updateTime();

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ <h1>Introduction</h1>
8585
<h3>Basic Example</h3>
8686
<div class="bs-docs-example">
8787
<p>This simple directive <code>&lt;timer /&gt;</code> markup code will start the timer with the default option of ticking every 1 millisecond</p>
88-
<div><timer id="timer1"/></div>
88+
<h3><timer id="timer1"/></h3>
8989
<button type="button" class="btn" onclick="startTimer('timer1', this)">Start</button>
9090
<button type="button" class="btn" onclick="stopTimer('timer1', this)">Stop</button>
9191
</div>
@@ -95,7 +95,7 @@ <h3>Basic Example</h3>
9595
<h3>Timer with interval example</h3>
9696
<div class="bs-docs-example">
9797
<p>This directive <code>&lt;timer interval="1000" id="timer2"/&gt;</code> markup code will run the timer tick every second</p>
98-
<div><timer interval="1000" id="timer2"/></div>
98+
<h3><timer interval="1000" id="timer2"/></h3>
9999
<button type="button" class="btn" onclick="startTimer('timer1', this)">Start</button>
100100
<button type="button" class="btn" onclick="stopTimer('timer1', this)">Stop</button>
101101
</div>
@@ -105,7 +105,7 @@ <h3>Timer with interval example</h3>
105105
<h3>Formatted timer</h3>
106106
<div class="bs-docs-example">
107107
<p>This directive <code>&lt;timer interval="1000" id="timer3"/&gt;</code> markup code will run the timer tick every second</p>
108-
<div><timer id="timer3">{{minutes}} minutes, {{seconds}} seconds.</timer></div>
108+
<h3><timer id="timer3">{{minutes}} minutes, {{seconds}} seconds.</timer></h3>
109109
<button type="button" class="btn" onclick="startTimer('timer3', this)">Start</button>
110110
<button type="button" class="btn" onclick="stopTimer('timer3', this)">Stop</button>
111111
</div>

0 commit comments

Comments
 (0)