diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index 6772d93243d9..717d17a6e159 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -76,9 +76,6 @@ function FormController(element, attrs, $scope, $animate, $interpolate) { parentForm.$addControl(form); - // Setup initial state of the control - element.addClass(PRISTINE_CLASS); - /** * @ngdoc method * @name form.FormController#$rollbackViewValue @@ -451,9 +448,12 @@ var formDirectiveFactory = function(isNgForm) { name: 'form', restrict: isNgForm ? 'EAC' : 'E', controller: FormController, - compile: function() { + compile: function ngFormCompile(formElement) { + // Setup initial state of the control + formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS); + return { - pre: function(scope, formElement, attr, controller) { + pre: function ngFormPreLink(scope, formElement, attr, controller) { if (!attr.action) { // we can't use jq events because if a form is destroyed during submission the default // action is not prevented. see #1238 diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index b8ceea7e546a..ae24ce8f5deb 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1756,11 +1756,6 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$ var parentForm = $element.inheritedData('$formController') || nullFormCtrl, currentValidationRunId = 0; - // Setup initial state of the control - $element - .addClass(PRISTINE_CLASS) - .addClass(UNTOUCHED_CLASS); - /** * @ngdoc method * @name ngModel.NgModelController#$setValidity @@ -2380,42 +2375,47 @@ var ngModelDirective = function() { restrict: 'A', require: ['ngModel', '^?form', '^?ngModelOptions'], controller: NgModelController, - link: { - pre: function(scope, element, attr, ctrls) { - var modelCtrl = ctrls[0], - formCtrl = ctrls[1] || nullFormCtrl; + compile: function ngModelCompile(element) { + // Setup initial state of the control + element.addClass(PRISTINE_CLASS).addClass(UNTOUCHED_CLASS).addClass(VALID_CLASS); - modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options); + return { + pre: function ngModelPreLink(scope, element, attr, ctrls) { + var modelCtrl = ctrls[0], + formCtrl = ctrls[1] || nullFormCtrl; - // notify others, especially parent forms - formCtrl.$addControl(modelCtrl); + modelCtrl.$$setOptions(ctrls[2] && ctrls[2].$options); - attr.$observe('name', function(newValue) { - if (modelCtrl.$name !== newValue) { - formCtrl.$$renameControl(modelCtrl, newValue); - } - }); + // notify others, especially parent forms + formCtrl.$addControl(modelCtrl); - scope.$on('$destroy', function() { - formCtrl.$removeControl(modelCtrl); - }); - }, - post: function(scope, element, attr, ctrls) { - var modelCtrl = ctrls[0]; - if (modelCtrl.$options && modelCtrl.$options.updateOn) { - element.on(modelCtrl.$options.updateOn, function(ev) { - modelCtrl.$$debounceViewValueCommit(ev && ev.type); + attr.$observe('name', function(newValue) { + if (modelCtrl.$name !== newValue) { + formCtrl.$$renameControl(modelCtrl, newValue); + } }); - } - element.on('blur', function(ev) { - if (modelCtrl.$touched) return; + scope.$on('$destroy', function() { + formCtrl.$removeControl(modelCtrl); + }); + }, + post: function ngModelPostLink(scope, element, attr, ctrls) { + var modelCtrl = ctrls[0]; + if (modelCtrl.$options && modelCtrl.$options.updateOn) { + element.on(modelCtrl.$options.updateOn, function(ev) { + modelCtrl.$$debounceViewValueCommit(ev && ev.type); + }); + } + + element.on('blur', function(ev) { + if (modelCtrl.$touched) return; - scope.$apply(function() { - modelCtrl.$setTouched(); + scope.$apply(function() { + modelCtrl.$setTouched(); + }); }); - }); - } + } + }; } }; }; @@ -2970,8 +2970,9 @@ function addSetValidityMethod(context) { parentForm = context.parentForm, $animate = context.$animate; + classCache[INVALID_CLASS] = !(classCache[VALID_CLASS] = $element.hasClass(VALID_CLASS)); + ctrl.$setValidity = setValidity; - toggleValidationCss('', true); function setValidity(validationErrorKey, state, options) { if (state === undefined) {