0

I'm trying to make a rule for input, for example when the value is 0 then something, but default $viewValue is NaN. How to get a real value?

I can't use $watch or $parse because I need to use only first value of my input and activate my rule.

return {
            require: 'ngModel',
            link: function (scope, element, attr, ngModelCtrl) {

            console.log(ngModelCtrl);  // says that $viewValue exists
            console.log(ngModelCtrl.$viewValue); // says it's NaN

HTML

 {{metters.test = 0}}
<input type="text" ng-model="metters.test" mydirective>

Console.log

ngModelCtrl $viewValue :  NaN
ngModelCtrl $modelValue :  NaN

ngModelCtrl :  e.i…e.c {
$viewValue: NaN, 
$modelValue: NaN, 
$parsers: Array[0], 
$formatters: Array[0],
$modelValue: 0
$viewValue: 0
.... }
Odinn
  • 1,106
  • 9
  • 28
  • sorry for that, have just added – Odinn Dec 21 '15 at 10:41
  • it's written in comments. in first situation when you open ngModelCtrl you can see $viewValue = 0. in second situation onsole.log(ngModelCtrl.$viewValue ) is 'NaN' – Odinn Dec 21 '15 at 11:30
  • I've just saw that there is two different values in the 'ngModelCtrl', first is NaN, and the second is my number value. I've added a console log to my post. please see – Odinn Dec 21 '15 at 11:48
  • OK, so this http://stackoverflow.com/questions/24087888/javascript-array-is-nan-angularjs-ngmodel explains the **NaN**. It looks like you need a watcher to get the initial value http://stackoverflow.com/questions/18458383/angularjs-read-initial-value-of-ngmodel-viewvalue-from-different-directive. Good luck! – camden_kid Dec 21 '15 at 12:02

0 Answers0