how to get ng-model value in directive

Rohit Hushare

I have written a directive in AngularJS, and used the input type number with min-max and ng-model attribute. I have used isolated scope. In the directive, I have written a blur event. I am getting min-max value but not getting ng-model value:

<input my-directive min="5" max="10" ng-model="num" type="number">
myApp.directive('myDirective', function () {
  function link($scope, ele, attr) {
    $scope.$watch('num', function (value) {
      console.log(value);
    })
    ele.on('blur', function () {
      console.log($scope.num, $scope.min, $scope.max);
    })
  }
  return {
    restrict : 'A',
    scope: {
      num: "=ngModel",
      min: "=?",
      max: "=?"
    },
    link: link
  }
});

output: undefined 5 10

What am I doing wrong?

Dan

Instead of using a binding to ngModel, require it and inject it into the link function:

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
  }
});

Here is a demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to access ng-model value in directive?

how to get ng-class value in directive

How to change ng-model scope value in custom Angular directive?

How to intercept value binding by ng-model directive

Angular: how to get a template ng-model into a directive for filtering

How to get selected value from ng-autocomplete directive to controller

change ng-model value from directive

pass the ng-model value to angular directive

Passing the value in directive to the ng-model. -angularjs

AngularJS : how to get directive input field model value in controller?

How to modify a model(ng-model value) specified behind a directive in angularjs?

md-checkbox does not get set by ng-model value inside a directive's link() function

Angularjs: How to get value of input without ng-model

Angular Directive captures initial null value of ng-model

Pass ng-model and place-holder value into directive

Binding value in md-dialog to ng-model in directive

Cannot set ng-model value to null from directive

Passing value from directive back to ng-model

How to get value of directive in angularjs

How to pass ng-model from directive to template

How to access ng-model from attribute directive?

How to make a directive update ng-model on jquery on event?

How to set ng-model-options in a custom directive?

How to set ng-model variable from custom validation directive

How to build Custom directive similar to ng-model?

ng-model and dropdown, get name, not value

Get ng-model from a input inside directive and put it in an attribute of directive

AngularJS : ng-model directive

How to get value when model value lower than ng-min?