javascript - $scope var 有多个相互矛盾的值?

标签 javascript angularjs

好吧,这是一个我以前从未遇到过的奇怪问题,而且我在 Internet 上也没有看到其他人遇到过这个问题;

对于初学者,我正在使用 AngularJS$scope 变量。

在使用 AngularJS 时,我注意到我的变量同时显示了在 Controller 开头设置的值,例如:

$scope.editMode = true;

以及我在 Controller 内部的函数中为它们设置的值,在这种情况下,该函数是通过单击触发的。例如:

$scope.disableEditMode = function() {$scope.editMode = false;}

我在 Controller 中运行一秒钟的时间间隔来检查变量的值,它告诉我它是真的然后是假的,等等......

Why is it changing by its own? Or why is it having both of the values? What's going on here?

顺便说一句,我不确定这是否是这个问题的原因,但是在更改变量值的点击事件上,我还将用户重定向到另一个使用相同 Controller 的页面,我认为是这样可能会重置变量的数据,但显然不是这种情况。

谢谢。

最佳答案

普通的 javascript setInterval() 不是 Angular 范围的一部分。

Angular 有它自己的实现,$interval,它将正确评估范围。

var timer = $interval(function() {
                console.log($scope.editMode);
            }, 1000);

你必须在 Controller 中包含这个:

...controller('myController', function($scope, $interval)..

此外,要检查/更正的另一件事是范围变量,如果它以某种方式在子 Controller 中并用作 ng-model。 更多信息:

关于javascript - $scope var 有多个相互矛盾的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40589269/

相关文章:

javascript - 将 POST 数据 react 到 ASP.Net

Javascript 七段显示器不亮起或响应事件点击

javascript - jQuery 精简版/AngularJS : How to add a class to a single child of an element?

java - 通过 AngularJS 使用 RESTful Web 服务

angularjs - 如何从 Protractor 中的 ng-repeat 获取值?

javascript - 在单独的文件中访问 React 组件的 props

javascript - 在 javascript 中使用 ModelAndView 对象属性

javascript - 检测用户是否喜欢我的 FB 页面,并删除 DIV

javascript - AngularJS 中的数组属性转换

angularjs - grid.appScope 不适用于 ui grid angular js