(isNgForm)
| 21455 | * related scope, under this name. |
| 21456 | */ |
| 21457 | var formDirectiveFactory = function(isNgForm) { |
| 21458 | return ['$timeout', '$parse', function($timeout, $parse) { |
| 21459 | var formDirective = { |
| 21460 | name: 'form', |
| 21461 | restrict: isNgForm ? 'EAC' : 'E', |
| 21462 | require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form |
| 21463 | controller: FormController, |
| 21464 | compile: function ngFormCompile(formElement, attr) { |
| 21465 | // Setup initial state of the control |
| 21466 | formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS); |
| 21467 | |
| 21468 | var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false); |
| 21469 | |
| 21470 | return { |
| 21471 | pre: function ngFormPreLink(scope, formElement, attr, ctrls) { |
| 21472 | var controller = ctrls[0]; |
| 21473 | |
| 21474 | // if `action` attr is not present on the form, prevent the default action (submission) |
| 21475 | if (!('action' in attr)) { |
| 21476 | // we can't use jq events because if a form is destroyed during submission the default |
| 21477 | // action is not prevented. see #1238 |
| 21478 | // |
| 21479 | // IE 9 is not affected because it doesn't fire a submit event and try to do a full |
| 21480 | // page reload if the form was destroyed by submission of the form via a click handler |
| 21481 | // on a button in the form. Looks like an IE9 specific bug. |
| 21482 | var handleFormSubmission = function(event) { |
| 21483 | scope.$apply(function() { |
| 21484 | controller.$commitViewValue(); |
| 21485 | controller.$setSubmitted(); |
| 21486 | }); |
| 21487 | |
| 21488 | event.preventDefault(); |
| 21489 | }; |
| 21490 | |
| 21491 | addEventListenerFn(formElement[0], 'submit', handleFormSubmission); |
| 21492 | |
| 21493 | // unregister the preventDefault listener so that we don't not leak memory but in a |
| 21494 | // way that will achieve the prevention of the default action. |
| 21495 | formElement.on('$destroy', function() { |
| 21496 | $timeout(function() { |
| 21497 | removeEventListenerFn(formElement[0], 'submit', handleFormSubmission); |
| 21498 | }, 0, false); |
| 21499 | }); |
| 21500 | } |
| 21501 | |
| 21502 | var parentFormCtrl = ctrls[1] || controller.$$parentForm; |
| 21503 | parentFormCtrl.$addControl(controller); |
| 21504 | |
| 21505 | var setter = nameAttr ? getSetter(controller.$name) : noop; |
| 21506 | |
| 21507 | if (nameAttr) { |
| 21508 | setter(scope, controller); |
| 21509 | attr.$observe(nameAttr, function(newValue) { |
| 21510 | if (controller.$name === newValue) return; |
| 21511 | setter(scope, undefined); |
| 21512 | controller.$$parentForm.$$renameControl(controller, newValue); |
| 21513 | setter = getSetter(controller.$name); |
| 21514 | setter(scope, controller); |
no test coverage detected