Вместо использования привязки к ngModel
, require
оно и внедрение его в функцию ссылки:
myApp.directive('myDirective', function () {
function link($scope, ele, attr, ngModel) { // <--- inject
$scope.$watch(() => ngModel.$viewValue, (n, o) => {
// watching `ngModel.$viewValue`
console.log('WATCH', n, o);
})
ele.on('blur', function () {
// `ngModel.$viewValue` is the current value
console.log('BLUR', ngModel.$viewValue, $scope.min, $scope.max);
})
}
return {
require: 'ngModel', // <--- require
restrict : 'A',
scope: { // <--- remove `ngModel` binding from scope
min: "=?",
max: "=?"
},
link: link
}
});
Вот демо