Radu Andrei
Radu Andrei

Reputation: 1073

angular form name from inside associated controller

I'm very new to angular!! What i'm trying to do is get the form name from inside the associated controller or a reference to the form object from inside the controller.

<form name="someName" ng-controller="formController">
    <label>Name:
        <input type="text"/>
    </label>
    <input type="submit"/>
</form>

controller:

obApp.controller('formController',function($scope){
  //this does NOT work - undefined - was expecting it to be "someName"
  var q = $scope.formName;
  //this exists - but can not use it since the 
  //name of the form can be whatever and i do not know beforehand what that name is
  var name = $scope.someName.$name;
});

My problem is that i don't know how in the world to get the name of the actual form. A reference to it would be even better. Looked for about 5 hours and i can't seem to figure it out.

The problem translates to this: "How to get a reference to the form object form the associated controller?".

Upvotes: 1

Views: 105

Answers (1)

Josep
Josep

Reputation: 13071

The thing is that you want to access the DOM element of the controller, you could do it like this:

obApp.controller('formController',function($scope, $element){
  var name = $element.attr('name');
});

But that is considered a bad practice, you shouldn't be accessing the DOM element directly inside the controller, consider using a directive instead.

Upvotes: 2

Related Questions