如何在 $scope 对象处自动触发我的变量?
//controller
setInterval(function(){$scope.rand=Math.random(10)},1000);
//template
{{rand}}
兰德没有在我的页面上更新。如何更新我的变量?
如何在 $scope 对象处自动触发我的变量?
//controller
setInterval(function(){$scope.rand=Math.random(10)},1000);
//template
{{rand}}
兰德没有在我的页面上更新。如何更新我的变量?
function MyCtrl($scope, $timeout) {
  $scope.rand = 0;
  (function update() {
    $timeout(update, 1000);
    $scope.rand = Math.random() * 10;
  }());
}
演示:http: //jsbin.com/udagop/1/
实际上,最Angularish的方法是:
function MyCtrl($scope, $interval) {
  $scope.rand = 0;
  function update() {
    $scope.rand = Math.random() * 10;
  }
  $interval(update, 1000);
}
这就是 setInterval() 的 Angular 等价物
你可以这样做:
//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>