RONE
RONE

Reputation: 5485

Need to know, why $apply need to be used

I need to know, why we need to used $apply here,

var clock = angular.module('clock',[]); 
clock.controller('MyController', function($scope) {
        $scope.clock = new Date();
        var updateClock = function() {
          $scope.clock = new Date();
        };
        setInterval(function() {
          $scope.$apply(updateClock);
          //updateClock()  //why this will not work.
        }, 1000);
        updateClock();
      });

Demo

I know, when $apply is used :

"The $apply() function executes an expression inside of the Angular context from outside of the Angular framework. For instance, if we implement a setTimeout() or are using a third-party library and we want the event to run inside the Angular context, we must use $apply()." Blockquote

But why we need to use in this case (I mean in jsfiddle example), As I am in the same context and not using any jQuery code or third-party library,

With Out $apply fiddle

function is called, but the output is reflected in view, Why?

Upvotes: 2

Views: 120

Answers (1)

Miraage
Miraage

Reputation: 3464

Read the following manual. :)
https://github.com/angular/angular.js/wiki/When-to-use-$scope.$apply()

// Upd from comments
You need to use $interval service.

Upvotes: 1

Related Questions