(isNgForm)
| 24238 | * related scope, under this name. |
| 24239 | */ |
| 24240 | var formDirectiveFactory = function(isNgForm) { |
| 24241 | return ['$timeout', '$parse', function($timeout, $parse) { |
| 24242 | var formDirective = { |
| 24243 | name: 'form', |
| 24244 | restrict: isNgForm ? 'EAC' : 'E', |
| 24245 | require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form |
| 24246 | controller: FormController, |
| 24247 | compile: function ngFormCompile(formElement, attr) { |
| 24248 | // Setup initial state of the control |
| 24249 | formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS); |
| 24250 | |
| 24251 | var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false); |
| 24252 | |
| 24253 | return { |
| 24254 | pre: function ngFormPreLink(scope, formElement, attr, ctrls) { |
| 24255 | var controller = ctrls[0]; |
| 24256 | |
| 24257 | // if `action` attr is not present on the form, prevent the default action (submission) |
| 24258 | if (!('action' in attr)) { |
| 24259 | // we can't use jq events because if a form is destroyed during submission the default |
| 24260 | // action is not prevented. see #1238 |
| 24261 | // |
| 24262 | // IE 9 is not affected because it doesn't fire a submit event and try to do a full |
| 24263 | // page reload if the form was destroyed by submission of the form via a click handler |
| 24264 | // on a button in the form. Looks like an IE9 specific bug. |
| 24265 | var handleFormSubmission = function(event) { |
| 24266 | scope.$apply(function() { |
| 24267 | controller.$commitViewValue(); |
| 24268 | controller.$setSubmitted(); |
| 24269 | }); |
| 24270 | |
| 24271 | event.preventDefault(); |
| 24272 | }; |
| 24273 | |
| 24274 | formElement[0].addEventListener('submit', handleFormSubmission); |
| 24275 | |
| 24276 | // unregister the preventDefault listener so that we don't not leak memory but in a |
| 24277 | // way that will achieve the prevention of the default action. |
| 24278 | formElement.on('$destroy', function() { |
| 24279 | $timeout(function() { |
| 24280 | formElement[0].removeEventListener('submit', handleFormSubmission); |
| 24281 | }, 0, false); |
| 24282 | }); |
| 24283 | } |
| 24284 | |
| 24285 | var parentFormCtrl = ctrls[1] || controller.$$parentForm; |
| 24286 | parentFormCtrl.$addControl(controller); |
| 24287 | |
| 24288 | var setter = nameAttr ? getSetter(controller.$name) : noop; |
| 24289 | |
| 24290 | if (nameAttr) { |
| 24291 | setter(scope, controller); |
| 24292 | attr.$observe(nameAttr, function(newValue) { |
| 24293 | if (controller.$name === newValue) return; |
| 24294 | setter(scope, undefined); |
| 24295 | controller.$$parentForm.$$renameControl(controller, newValue); |
| 24296 | setter = getSetter(controller.$name); |
| 24297 | setter(scope, controller); |
no test coverage detected