RiksonTool
RiksonTool

Reputation: 167

form.$valid undefined in angularJS

When I used angular form validation, it gives undefined error in angular controller.

My HTML View

Index.xhtml (Main View)

<div>
   <select ng-model="val">
      <option value="A"></option>
      <option value="B"></option>
      <option value="C"></option>
   </select>

      <div ng-if="item.value=='A'">
           @Html.Partial("A")
       </div>

       <div ng-if="item.value=='B'">
           @Html.Partial("B")
       </div>

       <div ng-if="item.value=='C'">
           @Html.Partial("A")
       </div>
 </div>

A.chtml (partial view)

<div>
   <form name="formA" ng-submit="A();">
     <div ng-model="model1" required>
     </div>
     <input type="submit" value="Add"/>
   </form>
</div>

B.chtml (partial view)

<div>
   <form name="formC" ng-submit="B();">
     <div ng-model="model2" required>
     </div>
     <input type="submit" value="Add"/>
   </form>
</div>

JS File

$scope.A = function(){
           if($scope.formA.$valid){
            }
    };

First I Select Value A in main view drop-down and, insert data and clicked Add button. Then it will checked the form validation. if validation true, function work success. But, after that I select value C i drop-down and try add inserted value, angular controller cannot identify the form name. It gives undefined error.

If I select B value after A selected, function execute without error. I tried to after set

 $scope.formA.$setPristine();
 $scope.formA.$setUntouched();

But It doesn't give any effect for this erro.

Can anyone help me? any explanation about internal functionality?

Upvotes: 0

Views: 1799

Answers (1)

Judy007
Judy007

Reputation: 5860

You must have $scope prefixed to formA, that was more than likely the issue

Upvotes: 0

Related Questions