Javascript 无法访问$watch内的作用域变量
我正在angularJS中开发一个过滤器。一切正常,但只有一个地方我无法访问scope对象,但当我更改代码时,它正常工作,但我想知道问题出在哪里。我认为我对scope和指令的理解是错误的 在一个指令中,我正在访问一个数组,在scope.watch函数中,我试图访问该数组,但它不起作用Javascript 无法访问$watch内的作用域变量,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,我正在angularJS中开发一个过滤器。一切正常,但只有一个地方我无法访问scope对象,但当我更改代码时,它正常工作,但我想知道问题出在哪里。我认为我对scope和指令的理解是错误的 在一个指令中,我正在访问一个数组,在scope.watch函数中,我试图访问该数组,但它不起作用 var currentScope=scope.mainList[scope.$index].list; //this will hit only when this corresponding scope var
var currentScope=scope.mainList[scope.$index].list;
//this will hit only when this corresponding scope variable changes
scope.$watch(currentScope,function (newVal) {
var hasTrue, hasFalse;
angular.forEach(newVal, function (v) {
if (v["isSelected"]) {
hasTrue = true;
} else {
hasFalse = true;
}
});
if (hasTrue && hasFalse) {
iElement.attr('checked', false);
// iElement.addClass('greyed');
} else {
iElement.attr('checked', hasTrue);
// iElement.removeClass('greyed');
}
}, true);
工作代码
var currentScope=scope.mainList[scope.$index].list;
//this will hit whenever any of the scope variable changes
scope.$watch(function () {
var hasTrue, hasFalse;
angular.forEach(currentScope, function (v) {
if (v["isSelected"]) {
hasTrue = true;
} else {
hasFalse = true;
}
});
if (hasTrue && hasFalse) {
iElement.attr('checked', false);
// iElement.addClass('greyed');
} else {
iElement.attr('checked', hasTrue);
// iElement.removeClass('greyed');
}
}, true);
这不起作用
var currentScope=scope.mainList[scope.$index].list;
//this will hit only when this corresponding scope variable changes
scope.$watch(currentScope,function (newVal) {
var hasTrue, hasFalse;
angular.forEach(newVal, function (v) {
if (v["isSelected"]) {
hasTrue = true;
} else {
hasFalse = true;
}
});
if (hasTrue && hasFalse) {
iElement.attr('checked', false);
// iElement.addClass('greyed');
} else {
iElement.attr('checked', hasTrue);
// iElement.removeClass('greyed');
}
}, true);
我需要第二个代码,但每当我更改任何变量时,它都不会命中,并且它将未定义
请检查下面的链接,它将提供更多的想法
根据@tobi的评论展开,以下是编写手表功能的推荐方法:
var currentScope=scope.mainList[scope.$index].list;
scope.$watch(function () { return currentScope; }, function (newVal) {
var hasTrue = false, hasFalse = false;
angular.forEach(newVal, function (v) {
if (v["isSelected"]) {
hasTrue = true;
} else {
hasFalse = true;
}
});
if (hasTrue && hasFalse) {
iElement.attr('checked', false);
// iElement.addClass('greyed');
} else {
iElement.attr('checked', hasTrue);
// iElement.removeClass('greyed');
}
});
或者,您可以执行以下操作:
scope.currentScope=scope.mainList[scope.$index].list;
scope.$watch('currentScope', function (newVal) {
var hasTrue = false, hasFalse = false;
angular.forEach(newVal, function (v) {
if (v["isSelected"]) {
hasTrue = true;
} else {
hasFalse = true;
}
});
if (hasTrue && hasFalse) {
iElement.attr('checked', false);
// iElement.addClass('greyed');
} else {
iElement.attr('checked', hasTrue);
// iElement.removeClass('greyed');
}
});
第二个代码中无法访问哪个变量?@Jigar7521:在第一个代码中,我们监视整个范围,但在第二个代码中,仅监视特定数组,但第二个数组未命中,请检查我的小提琴,您将理解问题。在第一个示例中,您没有监视整个范围。您正在观察提供的函数返回值的变化。请在此处阅读有关$watch的信息:。它首先接受一个表达式(字符串或函数),第二个参数是侦听器(当它检测到表达式中的更改时,由angular调用)。您可能已经有了这样的想法,即为范围中的每个更改调用您的代码,但是angular将多次调用此函数以检测表达式中的更改。@Tobi:那么解决方法是什么呢谢谢..它工作正常我以前的代码的问题是,只要任何范围中的任何更改都会影响到watch函数,现在问题解决了..你能解释为什么这一个范围。$watch(currentScope,function(newVal){}不是working@user它不起作用,因为您没有真正向$watch提供表达式。您提供的是一个已经计算过的结果,它永远不会更改。2ps给出的两个示例是我执行此操作的两种方法。或者提供一个字符串,其中包含一个可在当前范围内计算的表达式(可以是逻辑的,也可以只是上面所述对象的名称)或者您提供了一个函数,angular将多次调用该函数。一旦表达式或函数的返回值发生变化,angular将调用您的侦听器。