Q:

Trigger a function inside a directive by watching a variable

Assume I have the following directive:
angular.module("myModule", [])
.directive('myDirective', function ($timeout) {
    return {
        restrict: 'EA',          
        scope: {
            value: '=',
            valueIsValid: '='         
        },
        link: function (scope, element, attrs) { 

             scope.returnValueIsValid = function() {
                  // return valueIsValid = true or false 
             };
        }
    }
});

invoked the following way:
 <div my-directive  value="value"  value-is-valid="valueIsValid"></div>

The value variable is input to the directive, where the directive should return if value is valid or not in the valueIsValid variable. Is there a way to trigger the validation function by just watching valueIsValid ?

A:

I assume valueIsValid will be true when value>0

var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl',function($scope, $timeout) {

});

myApp.directive('myDirective', function ($timeout) {
    return {
        restrict: 'EA',          
        scope: {
            value: '=',
            valueIsValid: '='         
        },
        link: function (scope, element, attrs) { 
           scope.$watch("value",function(a,b){
           if(a && a != 0){
             scope.valueIsValid =true;
           }else{
              scope.valueIsValid =false;
              }
           })
        }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<input type="number" value="0" ng-model="value"/>
 <div my-directive  value="value"  value-is-valid="valueIsValid"></div>
 value is valid:-   {{valueIsValid}}
</div>

