How can I accurately check ngModelController validity?
Within my directive, I have a controller object. When I console.log the object from inside the directive, it displays:
console.log(ctrl)
$dirty: false
$invalid: true
$modelValue: ""
$name: undefined
$pristine: true
$valid: false
$viewValue: ""
...
Subsequently, when I use if( ctrl.$valid === true )
to log the object again, it shows the same output as before.
console.log(ctrl); //ctrl.$valid is false
if(ctrl.$valid == true) {
console.log(ctrl); //ctrl.$valid is false
}
In addition, upon inspecting the element, I notice that the correct ng-invalid
class is indeed applied.
I intend to create a plnkr to illustrate this issue, although duplicating it may prove challenging.
Update When I use console.log(ctrl.$valid)
, it returns true
. This clarifies why the condition passes, but not why the object form displays $valid
as false
.
Furthermore, I have created a plnkr showcasing a similar scenario without encountering the same problem. example