1

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"

4
  • Did you try $scope.name? Commented Apr 22, 2017 at 16:35
  • Sorry it's typing mistake actually it's <form name="{{myForm}}" novalidate> Commented Apr 22, 2017 at 16:36
  • Well, you wrote $scope.myForm = "validateForm";. What exactly did you expect that $scope.myForm would print actually? Something different from validateForm? Because if it was something different then I would be puzzled. Wouldn't you? Commented Apr 22, 2017 at 16:38
  • I want it should print the form object not "validateForm" Commented Apr 22, 2017 at 16:53

1 Answer 1

1

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`
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi I tried your solution but it's not printing the form object.. it just printing the string name attibute
@Vinothkanna check updated.. forgot to update code in my function.. It should be $scope[form]
Is there any other way to do it other than this?
@Vinothkanna what do you mean by anyother way & why this isn't good?
This is good when we have a submit button inside the same form but in my case i have lots of components. I have a buttons in a seperate component so ng-submit is not triggered
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.