16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scope<br />

Now, we need to start tickGenerator. This can be done using the run function of<br />

the module API, as shown in the following code snippet of the app.js file:<br />

parking.run(function (tickGenerator) {<br />

tickGenerator.start();<br />

});<br />

To receive the current date, freshly updated, we just need to call the $on function of<br />

any $scope object, as shown in the following code snippet of the parking.html file:<br />

{{tick | date:"hh:mm"}}<br />

Consider the following code snippet in the controllers.js file:<br />

parking.controller("parkingCtrl", function ($scope) {<br />

var listenToTick = function () {<br />

$scope.$on('TICK', function (event, tick) {<br />

$scope.tick = tick;<br />

});<br />

};<br />

listenToTick();<br />

});<br />

From now, after the listenToTick function is called, the controller's $scope<br />

object will start to receive a broadcast notification every 1000 ms, executing the<br />

desired function.<br />

To stop the tick, we need to send a broadcast in the other direction in order to make<br />

it arrive at $rootScope. This can be done by means of the $emit function, shown as<br />

follows in the parking.html file:<br />

{{tick | date:"hh:mm"}}<br />

Stop<br />

Consider the following code snippet in the controllers.js file:<br />

parking.controller("parkingCtrl", function ($scope) {<br />

$scope.stopTicking = function () {<br />

$scope.$emit("STOP_TICK");<br />

};<br />

var listenToTick = function () {<br />

$scope.$on('TICK', function (event, tick) {<br />

$scope.tick = tick;<br />

});<br />

};<br />

listenToTick();<br />

});<br />

[ 112 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!