在角度变化而更新范围的变量没有反映到用户 [英] scope variable updated in angular but change is not reflected to user

查看:140
本文介绍了在角度变化而更新范围的变量没有反映到用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置像这样一些角度变量:

I have some angular variables set like so:

$scope.pane = [];
$scope.pane.count = 0;
$scope.pane.max = 5;

然后,我在这样的HTML显示变量:

Then I display the variables in the HTML like this:

{{pane.count}}

这工作正常,并显示 0 预期。

This works fine and displays 0 as expected.

现在,如果我在任何时候更新变量,更新确实发生了(我可以看到使用的console.log),但在HTML中的印版不更新。

Now if I update the variable at any point, the update does happen (I can see using console.log) but the printed version in the HTML does not update.

例如

setTimeout(function(){
  $scope.pane.count = 1;
},1000);

我使用的角度V1.3。我在做什么错了?

I am using Angular v1.3. What am I doing wrong?

推荐答案

为了让有关变化的角度知道,你必须使用角的超时包装

In order to let angular know about changes, you have to use angular wrapper for timeout.

试试这个:

$timeout(function() {
  $scope.pane.count = 1;
}, 1000);

一般情况下,当你有一个结构的方形世界之外(如jQuery插件),改变价值观,你必须调用$范围。$ Apply(应用),让角度了解他们。

Generally, when you have a structure outside angular world(such as jQuery plugin) that changes values, you have to call $scope.$apply() to let angular know about them.

$scope.$apply();

这篇关于在角度变化而更新范围的变量没有反映到用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆