Angular:注意不在循环内工作(Angular: watch not working inside loop)

编程入门 行业动态 更新时间:2024-10-28 21:21:26
Angular:注意不在循环内工作(Angular: watch not working inside loop)

在角度上,如果我动态地注册观看事件(在我的情况下在for循环),手表不起作用。 请看一下小提琴。 有任何想法吗?

var myApp = angular.module('myApp',[]); function MyCtrl($scope, $parse) { // 1. binding watch inside loop (doesn't work): $scope.aaa = 1; $scope.bbb = 2; $scope.dependsOn = [ function () { return $scope.aaa; }, function () { return $scope.bbb; } ]; for (var i = 0; i < $scope.dependsOn.length; i++) { $scope.$watch( function () { return $scope.dependsOn[i]; }, function (newVal) { if (newVal !== undefined) { console.log("doesn't work"); } } ); } $scope.aaa = 5; $scope.bbb = 6; // binding watch not inside loop (works): $scope.ccc = 1; $scope.ddd = 2; $scope.dependsOn = [ function () { return $scope.ccc; }, function () { return $scope.ddd; } ]; $scope.$watch( function () { return $scope.dependsOn[0]; }, function (newVal) { if (newVal !== undefined) { console.log("works"); } } ); $scope.$watch( function () { return $scope.dependsOn[1]; }, function (newVal) { if (newVal !== undefined) { console.log("works"); } } ); $scope.ccc = 5; $scope.ddd = 6; }

小提琴

In angular if I register watch events dynamically (in my case in a for loop), the watch does not work. Please take a look at a fiddle. Any ideas?

var myApp = angular.module('myApp',[]); function MyCtrl($scope, $parse) { // 1. binding watch inside loop (doesn't work): $scope.aaa = 1; $scope.bbb = 2; $scope.dependsOn = [ function () { return $scope.aaa; }, function () { return $scope.bbb; } ]; for (var i = 0; i < $scope.dependsOn.length; i++) { $scope.$watch( function () { return $scope.dependsOn[i]; }, function (newVal) { if (newVal !== undefined) { console.log("doesn't work"); } } ); } $scope.aaa = 5; $scope.bbb = 6; // binding watch not inside loop (works): $scope.ccc = 1; $scope.ddd = 2; $scope.dependsOn = [ function () { return $scope.ccc; }, function () { return $scope.ddd; } ]; $scope.$watch( function () { return $scope.dependsOn[0]; }, function (newVal) { if (newVal !== undefined) { console.log("works"); } } ); $scope.$watch( function () { return $scope.dependsOn[1]; }, function (newVal) { if (newVal !== undefined) { console.log("works"); } } ); $scope.ccc = 5; $scope.ddd = 6; }

fiddle

最满意答案

您遇到的问题是因为您正在关闭中捕获变量i 。 然后i增加到值2并退出循环,你的每个委托将有2个,因为他们的实际执行委托的价值。

演示: http : //jsfiddle.net/df6L0v8f/1/ (添加:)

$scope.$watch( function () { console.log(i); return $scope.dependsOn[i]; }, function (newVal) { if (newVal !== undefined) { console.log("doesn't work"); } } );

您可以通过使用自调用闭包来解决此问题以及变量提升的问题,以便在迭代时捕获该值并为您的代理维护该值。

http://jsfiddle.net/df6L0v8f/4/

for (var i = 0; i < $scope.dependsOn.length; i++) { var watchDelegate = (function(itemDelegate){ return function () { return itemDelegate; }; })($scope.dependsOn[i]); $scope.$watch( watchDelegate, function (newVal) { if (newVal !== undefined) { console.log(newVal()); } } ); }

The problem you are experiencing is because you are capturing the variable i in a closure. Then i gets incremented to the value 2 and drops out of the loop, each of your delegates will have 2 as their i value upon actually executing the delegate.

Demonstrates: http://jsfiddle.net/df6L0v8f/1/ (Adds:)

$scope.$watch( function () { console.log(i); return $scope.dependsOn[i]; }, function (newVal) { if (newVal !== undefined) { console.log("doesn't work"); } } );

You can fix this and the issue of variable hoisting by using a self calling closure to capture the value at the time of iteration and maintain that for your delegate.

http://jsfiddle.net/df6L0v8f/4/

for (var i = 0; i < $scope.dependsOn.length; i++) { var watchDelegate = (function(itemDelegate){ return function () { return itemDelegate; }; })($scope.dependsOn[i]); $scope.$watch( watchDelegate, function (newVal) { if (newVal !== undefined) { console.log(newVal()); } } ); }

更多推荐

本文发布于:2023-08-03 16:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395183.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   Angular   watch   loop   working

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!