forgottofly
forgottofly

Reputation: 2719

Two models inside one watch function

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

Upvotes: 0

Views: 58

Answers (1)

Mukund Kumar
Mukund Kumar

Reputation: 23211

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
    }
});

Upvotes: 1

Related Questions