angularjs controller

call a function in a setinterval in the controller. http

by Akram kamal

HTML

<body ng-app="myApp">
<div  ng-controller='MyController'>
  <h5>{{ clock }}</h5>
    <h3>{{ person.name }}</h3> 
</div>
    <hr>
    <div ng-controller="DemoController">
  <h4>The simplest adding machine ever</h4>
  <button ng-click="add(1)" class="button">Add</button>
  <button ng-click="subtract(1)" class="button">Subtract</button>
  <h4>Current count: {{ counter }}</h4>
</div>
   <hr>
    <div ng-controller="Hello">
		<p>The ID is {{greeting.id}}</p>
		<p>The content is {{greeting.content}}</p>
   </div>          
</body>

JavaScript

var app = angular.module("myApp", []);

app.controller('MyController', function($scope) {
  $scope.person = { name: "Ari Lerner" };
  var updateClock = function() {
    $scope.clock = new Date();
  };
  var timer = setInterval(function() {
    $scope.$apply(updateClock);
  }, 1000);
  updateClock();
});

app.controller('DemoController', function($scope) {
  $scope.counter = 0;
  $scope.add = function(amount) { $scope.counter += amount; };
  $scope.subtract = function(amount) { $scope.counter -= amount; };
});

// from here : https://spring.io/guides/gs/consuming-rest-angularjs/
app.controller('Hello',function($scope, $http) {
    $http.get('http://rest-service.guides.spring.io/greeting').
        success(function(data) {
            $scope.greeting = data;
        });
});