$watch ngModel from inside directive using isolate scope

前端 未结 6 1870
刺人心
刺人心 2020-12-08 09:13

I am trying to watch my model value from inside my linking function.

scope.$watch(attrs.ngModel, function() {
       console.log(\"Changed\"); 
    });
         


        
6条回答
  •  执笔经年
    2020-12-08 09:33

    The proper way to do this is:

    app.directive('myDirective', function () {
      return {
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {
    
            ngModel.$render = function () {
                var newValue = ngModel.$viewValue;
                console.log(newValue)
            };
    
        }
      };
    });
    

提交回复
热议问题