如何观看array的array属性? [英] how to watch an array property of array of array?
问题描述
$scope.arr = [
[["TTT", 23],3],
[["PPP", 23],3],
[["KKK", 23],3]
];
我需要在数组的arr [0] [0] [0]元素上应用监视.
I need to apply watch on arr[0][0][0] element of array.
为所有数组使用ng-model到ng-repeat渲染文本框中的arr [0] [0] [0],arr [1] [0] [0],arr [2] [0] [0]
Rendering arr[0][0][0], arr[1][0][0], arr[2][0][0] in text box using ng-model through ng-repeat for all arrays.
一旦在文本框中输入内容,如何在ng-model变量上应用监视?
how to apply watch on ng-model variable as soon as i type something in text box?
我试图在整个数组arr上应用监视,但是它没有在监视功能下方触发
I tried to apply watch on entire array arr but it didn't trigger below watch function
$scope.$watch($scope.arr, function (newVal, oldVal)
{
$log.log(newVal);
}
);
html:
<div ng-repeat="el in arr">
Name: <input type="text" ng-model = "el[0][0]" />
</div>
推荐答案
似乎问题在于,对于 ng-repeat
的每次迭代都会生成一个新的作用域.为了解决这个问题,您可以为每个迭代附加一个单独的控制器,例如
It seems the issue lies in the fact that a new scope is being generated for each iteration of ng-repeat
. To get around this, you can attach a separate controller for each iteration, like explained here.
在没有多个控制器的情况下,我想到的最简单的方法是利用 ng-change
指令,如下所示:
The simplest way I can think of to get around this without multiple controllers is to utilize the ng-change
directive, like so:
JS:
$scope.getChanged = function(newValue) {
alert('getChanged(), new value: ' + newValue);
}
HTML:
<input type="text" ng-model="a[0][0]" ng-change="getChanged(a[0][0])" />
这是一个小提琴,它正在起作用.
Here's a fiddle showing it in action.
这篇关于如何观看array的array属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!