javascript - 一款 watch 功能内含两种型号

标签 javascript angularjs angularjs-scope watch

假设我有一个 watch 功能来观看这样的模型..

 $scope.$watch('number', function () {
   $scope.number = $scope.number.replace(/\D/, '');
   $scope.number = $scope.number.replace(/\s+/g, '');
   $scope.number =  $scope.number .replace(/[\W]/g, '');
   });

此功能限制用户在输入文本框中输入特殊字符和字母。

我还有另一个文本框,它使用另一个模型,例如 ng-model="faxNumber"

我可以在我的 watch 功能中添加此型号名称还是应该使用不同的 watch 功能?

谢谢

最佳答案

使用 $watchCollection 作为元素数组:

$scope.sortableItems = [
    {order: 1, text: 'foo'},
    {order: 2, text: 'bar'},

];

$scope.$watchCollection('sortableItems', function(newCol, oldCol, scope) {
    for (var index in newCol) {
        //apply operation
    }
});

关于javascript - 一款 watch 功能内含两种型号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668270/

相关文章:

javascript - Controller 继承在 AngularJS 中是一个好的实践吗?

javascript - AngularJS 在 1 个应用程序中声明多个 Controller

javascript - 如何在指令的 Controller 中访问父 Controller 的范围?

javascript - 遍历数组的最后几个条目

javascript - HTML5/JS : have the cursor locked in a canvas element

javascript - 通过Helper访问全局变量

javascript - AngularJS:如何使不属于 ng-repeat 一部分的局部变量起作用

javascript - 图片不会作为我的小 angularjs 应用程序的一部分显示

javascript - 范围更改不会出现在子范围中

javascript - 重构两个 jQuery UI 自动完成功能,使其更加实用和干燥