Vinoth kanna
Vinoth kanna

Reputation: 45

How to access dynamic form name in Controller?

I want to access the dynamic form name in controller. Here is my code:

HTML

<form name="{{myForm}}" novalidate>     
    <input type="text" ng-model="username" name="username" required/>
    <span ng-show="(submit && myForm.username.$error.required)">
        <span>Required</span>
    </span>
</form>

CONTROLLER

angular.module("myApp",[]).controller("myCtrl",function($scope) {
    $scope.myForm= "validateForm";      
    console.log("form" + $scope.myForm)
});

I want $scope.myForm should print the form object but it prints the string "validateForm"

Upvotes: 1

Views: 397

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

I'm guessing you wanted to validated dynamic form without knowing its name. You could pass a form name on form submit event ng-submit

<form name="{{name}}" novalidate ng-submit="save(name)">  

Then you could validate form inside controller method.

$scope.save = function(form){
   console.log($scope[form]) //here you can get form from `$scope`
}

Upvotes: 1

Related Questions