user1184100
user1184100

Reputation: 6894

Issues in attaching $scope.$watch to array items

I'm unable to attach $watch to a variable to check its validity within a form. fName.$valid throws error

TypeError: Cannot read property 'exp' of undefined
at watchFnToHumanReadableString

Array : menuItems

{
          name : 'Games',
          validationRequired : true,
          formName : 'games_action'
}

JS

angular.forEach($scope.menuItems, function(item,i) {
        if(item.validationRequired) {
            var fName = item.formName;

            $scope.$watch(fName.$valid, function(validity) {   /* throws error */
                domeSomething(validity);
            })


        }    
    });

Upvotes: 1

Views: 503

Answers (1)

angabriel
angabriel

Reputation: 5028

I guess its because fName is a String and has no own Property like $valid. But watching the string fName+'.$valid' should be possible.

$scope.$watch(fName+'.$valid', function(validity) {
    console.log('$watcher triggered: ', validity);
})

Upvotes: 1

Related Questions