2013-01-24 3 views
6

$ scope 객체에서 자동 변수를 자동으로 trig 처리 할 수 ​​있습니까?

//controller 
setInterval(function(){$scope.rand=Math.random(10)},1000); 

//template 
{{rand}} 

내 페이지에서 랜드가 업데이트되지 않습니다. 변수를 어떻게 업데이트합니까?

+0

$ scope 객체 란 무엇입니까? 그것은 텍스트 상자에 대한 변수가 무엇입니까? –

+0

@SyedSalmanRazaZaidi 그건 AngularJS입니다. – 11684

답변

10
function MyCtrl($scope, $timeout) { 
    $scope.rand = 0; 

    (function update() { 
    $timeout(update, 1000); 
    $scope.rand = Math.random() * 10; 
    }()); 
} 

데모 : 사실 http://jsbin.com/udagop/1/

3

당신은 할 수 :

//controller  
function UpdateCtrl($scope) { 
    $scope.rand = 0; 
    setInterval(function() { 
     $scope.$apply(function() { 
      $scope.rand = Math.random(10); 
     }); 
    }, 1000);    
} 

//template 
<div ng-controller="UpdateCtrl"> 
{{rand}}  
</div> 
6

할 수있는 가장 Angularish 방법이 될 것입니다 :

function MyCtrl($scope, $interval) { 
    $scope.rand = 0; 

    function update() { 
    $scope.rand = Math.random() * 10; 
    } 

    $interval(update, 1000); 
} 

이것은 setInterval()의 등가 값입니다.