angularjsangularjs-ng-form

How to get validation info of an input outside of a form?


We can get the validation info if the input is in a form: $scope.myForm.myField.$invalid etc..

What if input is outside of a form? How can we access the info?

I guess that field data (properties of the form object) is not same thing with ngModel. I tried something like this but didn't worked: (the model only contains string value of the input)

<input ng-model="myFakeForm.myField">

How can I achieve this?


Solution

  • Use the ng-form directive:

    <div ng-form="set1">
        <input name="field1" ng-model="myFakeForm.myField" ng-required="true" />
    </div>
    

    The input's ngModelController will be bound to $scope.set1.field1:

    console.log($scope.set1.field1.$valid);
    console.log($scope.set1.field1.$viewValue);
    //... etc
    

    For more information, see