1

when reseting the data in a form, wish to set form.setPristine(), but the formController is not yet registered in $scope.

this may seem like a dumb question, but how can I find the formController?

in the code below, get "TypeError: Cannot call method 'setPristine' of undefined"

index.html

<ng-form name='wordForm' ng-controller='wordCntl' >
  ...
</ng-form>

word.js

var langMod = angular.module('langMod', []);

langMod.controller( 'wordCntl', function($scope,$http,$location) {

  // data
  $scope.dflt = { wrd_id: '', usr_id: '', ln: '', word: '' };
  $scope.orig = {};
  $scope.data = {};

  // pull record default
  $scope.reset = function() {
    $scope.orig = angular.copy($scope.dflt);
    $scope.data = angular.copy($scope.orig);
    $scope.wordForm.setPristine();
  }

  $scope.reset();
};                          

the only way I know to get to the formController is when it's been set in the $scope. but it's not there yet, and I do not know how to find it.

4
  • Why don't you just check for $scope.wordForm for undefined\null before calling this method. If the form is not there once its loaded it would be in pristine state already. Commented Nov 12, 2013 at 4:55
  • @Chandermani - true, but what if I wanted to set the form dirty? Commented Nov 12, 2013 at 5:01
  • You can create a watch on wordForm and when it gets assigned the first time you can do whatever you want. Commented Nov 12, 2013 at 6:18
  • It is $setPristine() not setPristine(). Commented Nov 12, 2013 at 7:40

1 Answer 1

2

Directive controllers are shared among other directives. To access it, create a custom directive on the form and it becomes the 4th property to the link function.

Inside your custom directive you do this:

//inside custom directive
link: function(scope, element, attrs, controller){
  controller.$setPristine();
} 

I realize this is a very literal answer to your question. The real solution is probably not to call $scope.reset yet... why would you need to? Doesn't your form start off pristine anyway? If not, what is making it not pristine?

Sign up to request clarification or add additional context in comments.

4 Comments

an answer to your question, am using the reset() function both to initialize the data and as the onclick trigger to the reset button in the form. so yes, I could use a directive on the onclick which would both call reset() and setPristine()
thanks! your answer makes perfect sense, but is there another way to get to the formController without having to use a directive? or are we guaranteed that whenever we wish to use a formController that a directive would be appropriate? what if, for example, I wished to start off with a dirty form for some reason?
cannot make that work!! the controller parameter is undefined
The directive must live on the same element for the controller to be shared. The simplest solution for this is to not call $scope.wordForm.setPristine() initially. You can do it in subsequent calls to reset. Do this by checking for its existence in your reset function... as the form will start of pristine anyway, it should be fine. Your data will be set up and the form starts off pristine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.