我试图为图像绑定滚动,以便根据滚动位置,我可以更改$ scope值。但是,我正在做的事情是打破价值的约束。这是JSFiddle:
$scope.testFun = function(a) {
if (a.target.scrollLeft < 100) {
$scope.something.yes = true;
} else if (a.target.scrollLeft > 100) {
$scope.something.yes = false;
}
};
b.bind('scroll', $scope.testFun);
当&#34; something.yes&#34;更改(因为您已经滚动超过100px)它应该触发&#34; ng-show&#34;在文本&#34; SOMETHING的div上。&#34;不是。虽然,我知道他们最初的联系是因为某些事情在启动时具有适当的价值。
我不确定我是如何搞砸了&#34; $ scope.something&#34;
答案 0 :(得分:2)
即使你正在调用$scope
函数,它在技术上也不属于摘要循环,因为它在事件监听器上。这类事情通常只在指令中完成。
要使其发挥作用,您只需要强制摘要:
function testFun(a) {
$scope.$apply(function() {
console.log("a", $scope.something);
if (a.target.scrollLeft < 100) {
$scope.something.yes = true;
} else if (a.target.scrollLeft > 100) {
$scope.something.yes = false;
}
});
}
b.bind('scroll', testFun);
答案 1 :(得分:0)
如果使用bind或on作为快捷方式添加事件侦听器(或者添加自己的侦听器),则需要在事件处理程序内自行触发$ scope。调用$ apply会导致运行$ digest,然后检查所有$ watchers设置以查看是否需要更新任何内容。
答案 2 :(得分:0)
你需要在$ scope.testFun中使用$scope.$apply()
,因为当它被绑定时它不是一个角度绑定
$scope.testFun = function(a) {
console.log("a", $scope.something);
if (a.target.scrollLeft < 100) {
$scope.something.yes = true;
} else if (a.target.scrollLeft > 100) {
$scope.something.yes = false;
}
$scope.$apply();
};
工作fiddle