使用角度js中的watch函数检查数组值是否存在

时间:2015-09-02 08:12:09

标签: angularjs

<textarea ng-model="statement" name="statement"></textarea>

当我在文本区域中键入一些值时,我可以使用

在cotroller中触发一个函数
var autosave = function(newVal, oldVa){
    console.log('inside the function');
}
$scope.$watch('statement', autosave);

这里我要检查值数组是否包含一些值,如果值存在,我想在控制器中触发一些函数

$scope.testAry =[];

我要推出一些价值

$scope.testAry.push({username: jenson});

这次数组包含值,所以我需要触发一个函数

   $scope.$watch('testAry', autosave); // not working 

如果数组包含至少一个值

,任何人都可以帮助我触发自动保存功能

1 个答案:

答案 0 :(得分:1)

传递第三个参数true以观察数组值:

$scope.$watch('testAry', autosave, true);

或者您可以使用$ watchCollection。