Javascript Can';t渲染模型';s属性
向我寻求帮助。 Somewhy,更改属性(timerValue)的值时无法刷新该属性。它确实渲染过一次 以下是html分区:Javascript Can';t渲染模型';s属性,javascript,angularjs,Javascript,Angularjs,向我寻求帮助。 Somewhy,更改属性(timerValue)的值时无法刷新该属性。它确实渲染过一次 以下是html分区: <div>{{ game.timerValue }}</div> {{game.timerValue} js: // Game status $scope.game = { "started" : false, "timerValue" : 60, "score" : 0, "question"
<div>{{ game.timerValue }}</div>
{{game.timerValue}
js:
// Game status
$scope.game = {
"started" : false,
"timerValue" : 60,
"score" : 0,
"question" : "? ? ?",
"message" : "If all options are set up, then you may start!",
"wrong" : ""
};
// Handle Start Button click
$scope.startGame = function () {
if($scope.game.timer) clearTimeout($scope.game.timer);
$scope.game.score = 0;
$scope.game.wrong = "";
$scope.game.message = "The game started!";
$scope.game.timer = setInterval(function() {
$scope.game.timerValue -= 1;
if( $scope.game.timerValue <= 0)
{
$scope.game.message = "Defeat! Time is out! Your score is " + $scope.game.score;
clearTimeout($scope.game.timer);
}
},1000);
};
//游戏状态
$scope.game={
“开始”:错误,
“timerValue”:60,
“分数”:0,
“问题”:“?”,
“消息”:“如果设置了所有选项,则可以启动!”,
“错误”:”
};
//手柄开始按钮点击
$scope.startGame=函数(){
if($scope.game.timer)clearTimeout($scope.game.timer);
$scope.game.score=0;
$scope.game.error=“”;
$scope.game.message=“游戏开始了!”;
$scope.game.timer=setInterval(函数(){
$scope.game.timerValue-=1;
如果($scope.game.timerValue,您的UI未更新的原因是因为您的游戏计时器逻辑在常规角度摘要周期之外运行。有一篇很好的文章对此进行了解释:
与其使用setInterval
,不如建议使用Angular。它是窗口的包装器。setInterval
可以免除您手动调用$scope.$apply
或“告诉Angular更新UI”的责任
使用$interval
的其他好处:
- 它会自动将回调包装在try/catch块中,让您在
- 它返回承诺,因此与传统回调方法相比,它与其他基于承诺的代码的互操作性更好。当回调返回时,返回的值用于解析承诺
另一种解决方案是显式调用insidesetInterval
通知Angular“模型数据已更改,请更新UI”。您的UI未更新的原因是因为您的游戏计时器逻辑在常规Angular摘要周期之外运行。有一篇很好的文章对此进行了解释:
与其使用setInterval
,不如建议使用Angular。它是窗口的包装器。setInterval
可以免除您手动调用$scope.$apply
或“告诉Angular更新UI”的责任
使用$interval
的其他好处:
- 它会自动将回调包装在try/catch块中,让您在
- 它返回承诺,因此与传统回调方法相比,它与其他基于承诺的代码的互操作性更好。当回调返回时,返回的值用于解析承诺
另一种解决方案是显式调用insidesetInterval
通知Angular“模型数据已更改,请更新UI”。您的UI未更新的原因是因为您的游戏计时器逻辑在常规Angular摘要周期之外运行。有一篇很好的文章对此进行了解释:
与其使用setInterval
,不如建议使用Angular。它是窗口的包装器。setInterval
可以免除您手动调用$scope.$apply
或“告诉Angular更新UI”的责任
使用$interval
的其他好处:
- 它会自动将回调包装在try/catch块中,让您在
- 它返回承诺,因此与传统回调方法相比,它与其他基于承诺的代码的互操作性更好。当回调返回时,返回的值用于解析承诺
另一种解决方案是显式调用insidesetInterval
通知Angular“模型数据已更改,请更新UI”。您的UI未更新的原因是因为您的游戏计时器逻辑在常规Angular摘要周期之外运行。有一篇很好的文章对此进行了解释:
与其使用setInterval
,不如建议使用Angular。它是窗口的包装器。setInterval
可以免除您手动调用$scope.$apply
或“告诉Angular更新UI”的责任
使用$interval
的其他好处:
- 它会自动将回调包装在try/catch块中,让您在
- 它返回承诺,因此与传统回调方法相比,它与其他基于承诺的代码的互操作性更好。当回调返回时,返回的值用于解析承诺
另一种解决方案是显式地在setInterval
内部调用,以通知Angular“模型数据已更改,请更新UI”。setInterval
在单独的线程中执行其工作(排序),因此Angular无法检测到它对属性所做的任何更改。您必须使用$scope.$apply(函数(){…
)包装修改属性的任何函数,以便Angular检测到它们并将这些更改推送到UI。setInterval
在单独的线程中工作(类似于),因此Angular无法检测到它对属性所做的任何更改。您必须使用$scope.$apply(函数(){…
)包装修改属性的任何函数,以便Angular检测到它们并将这些更改推送到UI。setInterval
在单独的线程中工作(类似于),因此Angular无法检测到它对属性所做的任何更改。您必须使用$scope.$apply(函数(){…
)包装修改属性的任何函数,以便Angular检测到它们并将这些更改推送到UI。setInterval
在单独的线程中工作(类似于),因此Angular无法检测它对属性所做的任何更改。您必须包装
$scope.game.timer = setInterval(function() {
$scope.game.timerValue -= 1;
if( $scope.game.timerValue <= 0)
{
$scope.game.message = "Defeat! Time is out! Your score is " + $scope.game.score;
clearTimeout($scope.game.timer);
}
$scope.$apply();
},1000);
$scope.startGame = function () {
if($scope.game.timer) clearTimeout($scope.game.timer);
$scope.game.score = 0;
$scope.game.wrong = "";
$scope.game.message = "The game started!";
$scope.game.timer = setInterval(function() {
$scope.game.timerValue -= 1;
$scope.$apply();
if( $scope.game.timerValue <= 0)
{
$scope.game.message = "Defeat! Time is out! Your score is " + $scope.game.score;
clearTimeout($scope.game.timer);
}
},1000);
};
$scope.startGame = function () {
if($scope.game.timer) $timeout.cancel($scope.game.timer);
$scope.game.score = 0;
$scope.game.wrong = "";
$scope.game.message = "The game started!";
$scope.game.timer = $timeout(function() {
$scope.game.timerValue -= 1;
if( $scope.game.timerValue <= 0)
{
$scope.game.message = "Defeat! Time is out! Your score is " + $scope.game.score;
$timeout.cancel($scope.game.timer);
}
},1000);
};