gpt4 book ai didi

javascript - 使用 Angular 不在 HTML 上更新值

转载 作者:行者123 更新时间:2023-11-30 12:26:57 25 4
gpt4 key购买 nike

我有一个随机生成值的 Controller

app.controller('detailReadingCtrl',function(){
var value = 0;
$scope.dispValue = 0;

setInterval(function(){
value = Math.floor(Math.random()*1000);
$scope.dispValue = value;
}
});

我的html是

<div>{{dispValue}}</div>

html 上的值未使用更改后的值进行更新。 dispvalue 在 Controller 中发生变化,但它没有在 html 中更新。我想在屏幕上看到每秒的值变化而无需刷新。

我尝试使用 $scope.$watch 和 $scope.$broadcast 似乎都不起作用。如果您对此有任何想法,请告诉我。

最佳答案

setInterval 不会触发 $digest 循环,而是使用 $interval 模块:

app.controller('detailReadingCtrl',function($scope, $interval){
var value = 0;
$scope.dispValue = 0;

$interval(function(){
value = Math.floor(Math.random()*1000);
$scope.dispValue = value;
});
});

代码中还有其他错误($scope 没有被注入(inject),缺少 ) 在间隔结束时)

关于javascript - 使用 Angular 不在 HTML 上更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29037022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com