Amb
Amb

Reputation: 3353

how to $watch multiple variables in angularjs?

Here is a angularjs directive to show multiple form validation errors.
directive code -

app.directive('validationErrors', function($compile) {
    return({
        link : function($scope, el, attr) {

            $scope.fld = attr.id;
            $scope.individualValidationErrors = [];
            var model = ((attr.ngModel).split('.'))[0];

            $scope.validationErrors = {};
            $scope.validationErrors[model] = {};
            $scope.validationErrors[model][$scope.fld] = "";

            var html = $compile(
                '<div id="error-{{fld}}" style="color:red;">'+
                    '<ul>' +
                        '<li ng-repeat="error in individualValidationErrors[fld]">'+
                            '{{error}}' +
                        '</li>' +
                    '</ul>' +
                '</div>'
            )($scope);
            $('input[id="'+$scope.fld+'"]').after(html);

            $scope.$watch('validationErrors',
                function(newV) {
                    $scope.fld = attr.id;
                    $scope.individualValidationErrors = [];

                    console.log(newV);
                    console.log($scope.validationErrors);

                    if ($scope.fld != undefined) {
                        $scope.individualValidationErrors[$scope.fld] = $scope.validationErrors[model][$scope.fld];
                        //console.log($scope.individualValidationErrors);
                    }
                },
                true
            );
        }
    });
});

Html code -

<form ng-submit="registration()">
    <input validation-errors="validationErrors" maxlength="50" type="text" id="first_name" ng-model="User.first_name">
    <input validation-errors="validationErrors" maxlength="50" type="text" id="last_name" ng-model="User.last_name">
    <input validation-errors="validationErrors" maxlength="50" type="text" id="email" ng-model="User.email">
    <input validation-errors="validationErrors" type="password" id="password"  ng-model="User.password">
    <input class="btn btn-info" type="submit" id="registration-sbmit" value="Submit">
</form>

the error of the last field of form overwrites all fields in the form and so it is not showing individual error for field.. $scope.validationErrors variable is set in controller which i want to $watch in directive.

Upvotes: 1

Views: 1916

Answers (2)

btm1
btm1

Reputation: 3856

You can do it like this:

$scope.$watch('[myproperty1,myproperty2,myproperty3]',function(nv,ov){
//do some stuff
});

Upvotes: 0

Jason Aden
Jason Aden

Reputation: 2823

I think your primary issue here is that you are overwriting $scope.validationErrors in your link function. The link function will be run for every directive on the page. They are also sharing the same $scope object. So you need to conditionally create $scope.validationErrors and conditionally add top-level keys to it:

if(!$scope.validationErrors)
    $scope.validationErrors = {};
if(!$scope.validationErrors[model])
    $scope.validationErrors[model] = {};
$scope.validationErrors[model][$scope.fld] = "";

That should at least clear up your issue where you are only getting the last item in $scope.validationErrors.

Upvotes: 1

Related Questions