javascript - Angular JS。在 html 中自动刷新 $scope 变量

标签 javascript html templates angularjs

如何在 $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/

关于javascript - Angular JS。在 html 中自动刷新 $scope 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14500602/

相关文章:

javascript - 如何使用 java、jquery 或 javascript 访问扫描仪和扫描文档

javascript - 如何获取元素的类名

JavaScript : Creating a dynamic table with column title on the first row and row title in the first column

html - Ng-include 中的文本不突出显示

c++ - 如何在模板代码中使用编译时常量条件避免 "conditional expression is constant"警告?

javascript - 如何防止同一系统的多个用户生成相同的引用号

javascript - 陷阱 when.js 未处理的拒绝

javascript - 浏览器是读取/存储 Javascript 文件中的所有内容还是只读取/存储它们需要的内容?

c++ - 用于添加和比较 Integer 和 Fraction 类数据的运算符重载

c++ - 模板函数特化的单独声明和定义 : different behaviour for member and nonmember functions