0

Suppose if I have a watch function that watches a model like this..

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

This function restricts user from entering special characters and alphabets in the input text box.

And I have another text box which uses another model say ng-model="faxNumber"

Can I add this model name as well in my watch function or should I use a different watch function?

Thanks

forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • I think a better approach will be is to set the type as `number` and then use validators to validate the value than updating the vlaue – Arun P Johny Oct 31 '14 at 05:52

1 Answers1

1

use $watchCollection for array of element:

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

];

$scope.$watchCollection('sortableItems', function(newCol, oldCol, scope) {
    for (var index in newCol) {
        //apply operation
    }
});
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79
  • Thanks @Mukund Kumar,but if I have another set of elements can I add that element name inside this watch function? – forgottofly Oct 31 '14 at 06:38
  • no. $watchCollection can be used of only one set of element.we have to write other $watchCollection for other element. – Mukund Kumar Oct 31 '14 at 06:43