0

Why is $scope.watch not working with 'model'? http://jsfiddle.net/lesouthern/8PUtP/5/

.directive('testDirective',function() {
    return {
        restrict : 'A',
        scope : {
            model : '=ngModel'
        },
        link : function($scope,$element,$attrs) {
            $scope.$watch('model',function(x) {
                console.log('this is model: ' + x);
            });
        }
    }
});
koolunix
  • 1,995
  • 16
  • 25

1 Answers1

0

Please see this question. Here is your corrected code :

return {
    restrict : 'A',
    link : function($scope, $element, $attrs) {
        $scope.$watch($attrs.ngModel, function(x) {
            console.log('this is model: ' + x);
        });
    }
}
Community
  • 1
  • 1
Blackhole
  • 20,129
  • 7
  • 70
  • 68
  • Thank you for your anwser. The issue with this solution is that I need to restrict scope in this directive. How do I watch the value of this input, and have two way data binding with this scope variable? – koolunix Jun 19 '13 at 00:36
  • @koolunix IMHO, you should accept this answer because you used the exact same solution in your answer. – Stewie Jun 19 '13 at 04:47
  • the complete question had the requirement of limiting the scope of the directive. what is shown above does not answer this – koolunix Jun 19 '13 at 05:28
  • 2
    Notice that your solution doesn't restrict the scope neither. [This answer](http://stackoverflow.com/a/14116498/2057033) (and the related comments) will give you more details about what to do. – Blackhole Jun 19 '13 at 10:36
  • @Blackhole you are right. Thanks for your input. Here is an updated answer based on that link: http://jsfiddle.net/lesouthern/WN6aC/2/ – koolunix Jun 19 '13 at 16:42