如何在AngularJs中将vm传递给setTimeout?范围的更改不会更新DOM视图 [英] How to pass vm to a setTimeout in AngularJs? Changes to scope don't update DOM view
本文介绍了如何在AngularJs中将vm传递给setTimeout?范围的更改不会更新DOM视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以下代码.当我看到该组件出现/消失时,一切似乎都在起作用,这取决于我为变量设置的值.但是,当我在setTimeout(...)
函数中执行此操作时,它开始出现异常.显示 poofy 文本,但没有显示设置为vm
的值.我的猜测是我需要以某种方式通过它,但我不确定如何通过.
I'm trying the following code. Everything seems to be working as I see the component appear/disappear depending on the value I set for the variable. However, when I do that in the setTimeout(...)
function, it starts to misbehave. The poofy text shows but the value set to vm
doesn't. My guess is that I need to pass it in somehow but I'm not sure how.
(function () {
'use strict';
angular
.module("app")
.controller("Banana", banana);
function banana() {
var vm = this;
vm.showStuff = false;
setTimeout(function () {
console.log("poof!");
vm.showStuff = true;
}, 1000);
}
})();
我需要使视图模型可全局访问吗?
Do I need to make the view-model globally accessible?
推荐答案
尝试使用下面的脚本.
(function () {
'use strict';
angular
.module("app")
.controller("Banana", banana);
function banana($timeout) {
var vm = this;
vm.showStuff = false;
$timeout(function () {
console.log("poof!");
vm.showStuff = true;
}, 1000);
}
})();
要注意-需要执行其他步骤.
To be noted - there's additional step required.
- 用
setTimeout(...)
代替$timeout(...)
. - 将
$timeout
传递到banana(...)
. - 提供
banana.$inject = ["$timeout",...]
.
- Substitute
setTimeout(...)
for$timeout(...)
. - Pass
$timeout
intobanana(...)
. - Provide
banana.$inject = ["$timeout",...]
.
这篇关于如何在AngularJs中将vm传递给setTimeout?范围的更改不会更新DOM视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文