假设我有一个 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/