I ran into the same problem. Unfortunately, I couldn’t easily apply the Chandermani solution, because I need to access the form name from a call to $on within the parent area of ng-switch . Thus, I resorted to creating a directive that sends the form name to $rootScope :
.directive("globalName", ["$rootScope", function($rootScope) { return function(scope, element) { $rootScope["get_" + element.attr('name')] = function() { return scope[element.attr('name')]; }; } }]);
Usage looks like this:
<form name="whatever" novalidate global-name>...</form>
and then you get access to the form in controllers, for example. eg:
$scope.get_whatever().$setPristine(); $scope.get_whatever().$setUntouched();
Being a name in $rootScope , it no longer depends on your DOM structure.
I understand that this is not an optimal solution because it pollutes the global namespace, but I feel uncomfortable with the visibility of the form name depending on the structure of the DOM in several unexpected ways.
source share