0

http://jsfiddle.net/ccLwj3us/

Hi. this is my code. I need get the value of the input text, but I only can get the value, when the value on the input text ischanged. How can get the value of the input text without need change.(get initially the value of the input text)

I want to do something very generic, that I can reuse it in any controller. Have a certain number of fields and validate them, so I do not want to always get the value of the variable "number".

I want to reuse this directive on any controller. I will not always have the $ scope.numero variable. How can I get the value of the text field without typing scope.numero in the directive?

  var app = angular.module('app', []);

  app.controller('appCtrl', function ($scope) {
  $scope.numero=100500
  });

  app.directive('validate', function () {

      return {
          restrict: 'AE',
          require: 'ngModel', 

          link: function (scope, element, attrs, ngModel) {
            if (!ngModel){
              return;          
            }

            ngModel.$parsers.push(function(val){
              if ((val>10) && (val<20)){
                element.removeClass("wrong"); 
                element.addClass("correct"); 
              } else {
                element.removeClass("correct");
                element.addClass("wrong"); 
              }
              ngModel.$render();
            })


          }
      };
  });
12
  • you want to get the value of numero in your directive, when you are initializing it? Commented Mar 22, 2017 at 14:05
  • @Pramod_Para yes, i need this value but in the directive for check the validation to assign error | correct class Commented Mar 22, 2017 at 14:08
  • @Pramod_Para I want to do something very generic, that I can reuse it in any controller. Have a certain number of fields and validate them, so I do not want to always get the value of the variable "number" Commented Mar 22, 2017 at 14:18
  • Did you try setting an attribute and getting value in directive? Commented Mar 22, 2017 at 14:19
  • This is what I need, get the "value" attribute of the field Commented Mar 22, 2017 at 14:20

1 Answer 1

2

The data you need is already inside the ngModel, so you can retrieve it with ngModel.$modelValue.

(In general, when using Angular it's best to get out of the habit of reading data from the DOM -- the DOM is a side effect representation of the scope data; there's no need for the directive to "read the data from the input field" because the input field was filled using data already in the directive in the first place!)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks man, but the result of ngModel.$modelValue is NaN.. jsfiddle.net/0tsvue0L
You're trying to access the data before it's reached the directive. Wrap that alert in a $timeout (to force it to wait until the next $digest) and you'll see the correct value: jsfiddle.net/tL4b13o6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.