I have an input and on ng-change i pass the Form.input in a function so i can do some stuff based on the inputs status (errors, valid/invalid...). But here is the weird thing:
When i log the input i passed, i see in the console output that input.$valid = false which is the correct value. But when i log input.$valid itself i get that it is true which is false! Why is this happening??? When i try to parse input.$valid i get wrong value and i cannot do calculations based on that.
Here is the input:
<input ng-model="..."
type="radio"
name="input"
ng-value={{v}}
ng-disabled="field.readonly"
ng-required="field.required"
ng-init="test(Form.input)"
ng-change="test(Form.input)">
And here is the test function:
$scope.test = function (object) {
console.log(object);
console.log(object.$valid);
console.log(JSON.stringify(object));
};
and here is the log output:
// console.log(object);
c {$viewValue: NaN, $modelValue: NaN, $parsers: Array[1], $formatters: Array[1], $viewChangeListeners: Array[1]…}
$dirty: false
$error: Object
$formatters: Array[1]
$invalid: true
$isEmpty: function (a){return F(a)||""===a||null===a||a!==a}
$modelValue: undefined
$name: "input"
$parsers: Array[1]
$pristine: true
$render: function (){c[0].checked=d.value==e.$viewValue}
$setPristine: function (){this.$dirty=!1;this.$pristine=!0;g.removeClass(e,yb);g.addClass(e,Pa)}
$setValidity: function (a,c){p[a]!==!c&&(c?(p[a]&&n--,n||(k(!0),this.$valid=!0,this.$invalid=!1)):(k(!1),this.$invalid=!0,this.$valid=!1,n++),p[a]=!c,k(c,a),l.$setValidity(a,c,this))}
$setViewValue: function (d){this.$viewValue=d;this.$pristine&&(this.$dirty=!0,this.$pristine=!1,g.removeClass(e,Pa),g.addClass(e,yb),l.$setDirty());r(this.$parsers,function(a){d=a(d)});this.$modelValue!==
$valid: false
$viewChangeListeners: Array[1]
$viewValue: undefined
__proto__: Object
// console.log(object);
true
// console.log(JSON.stringify(object));
{"$viewValue":null,"$modelValue":null,"$parsers":[null],"$formatters":[null],"$viewChangeListeners":[null],"$pristine":true,"$dirty":false,"$valid":true,"$invalid":false,"$name":"input","$error":{}}
UPDATE: I think i might have found something. When i do:
<input ...
ng-init="{{test(Form.input)}}"
ng-change="{{test(Form.input)}}">
instead of
<input ...
ng-init="test(Form.input)"
ng-change="test(Form.input)">
i get the correct result, but i also get an error in console:
Error: $parse:syntax
Syntax Error: Token 'test' is at column {2} of the expression [{3}] starting at [{4}].
ng-initandng-changejust for testing purposes?Form.input.$validbeing true/false. But i need to make sure everything is working correctly so i usetest()forn now.$watchit to display the data and call your function it would be more effective.Form.input.$validi get the wrong value.