Implementation results:
Implementation code:
1 <!DOCTYPE HTML>2 <HTMLNg-app="">3 <Head><Scriptsrc= "./angular.min.js"></Script></Head>4 <Body>5 <H1>Timer Object</H1>6 <DivNg-controller= "ClockController">7 <h5>Time: {{clock}}</h5>8 </Div>9 <Scripttype= "Text/javascript">Ten functionClockController ($scope, $timeout) { One varUpdateclock= function(){ A $scope. Clock= NewDate (); - $timeout (function(){ - Updateclock (); the }, +); - }; - Updateclock (); - } + </Script> - </Body> + </HTML>
Improved notation (best practice: Reference binding in a view through object properties rather than the object itself, is a best practice in angular):
1 functionClockController ($scope) {2$scope. Clock ={3NowNewDate ()4 };5 varUpdateclock =function(){6$scope. Clock.now =NewDate ();7 };8SetInterval (function(){9 $scope. $apply (updateclock);Ten},1000); One A Updateclock (); -}
Angularjs Getting Started 3-small example-timer