(isNgForm)
| 24099 | * related scope, under this name. |
| 24100 | */ |
| 24101 | var formDirectiveFactory = function(isNgForm) { |
| 24102 | return ['$timeout', '$parse', function($timeout, $parse) { |
| 24103 | var formDirective = { |
| 24104 | name: 'form', |
| 24105 | restrict: isNgForm ? 'EAC' : 'E', |
| 24106 | require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form |
| 24107 | controller: FormController, |
| 24108 | compile: function ngFormCompile(formElement, attr) { |
| 24109 | // Setup initial state of the control |
| 24110 | formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS); |
| 24111 | |
| 24112 | var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false); |
| 24113 | |
| 24114 | return { |
| 24115 | pre: function ngFormPreLink(scope, formElement, attr, ctrls) { |
| 24116 | var controller = ctrls[0]; |
| 24117 | |
| 24118 | // if `action` attr is not present on the form, prevent the default action (submission) |
| 24119 | if (!('action' in attr)) { |
| 24120 | // we can't use jq events because if a form is destroyed during submission the default |
| 24121 | // action is not prevented. see #1238 |
| 24122 | // |
| 24123 | // IE 9 is not affected because it doesn't fire a submit event and try to do a full |
| 24124 | // page reload if the form was destroyed by submission of the form via a click handler |
| 24125 | // on a button in the form. Looks like an IE9 specific bug. |
| 24126 | var handleFormSubmission = function(event) { |
| 24127 | scope.$apply(function() { |
| 24128 | controller.$commitViewValue(); |
| 24129 | controller.$setSubmitted(); |
| 24130 | }); |
| 24131 | |
| 24132 | event.preventDefault(); |
| 24133 | }; |
| 24134 | |
| 24135 | formElement[0].addEventListener('submit', handleFormSubmission); |
| 24136 | |
| 24137 | // unregister the preventDefault listener so that we don't not leak memory but in a |
| 24138 | // way that will achieve the prevention of the default action. |
| 24139 | formElement.on('$destroy', function() { |
| 24140 | $timeout(function() { |
| 24141 | formElement[0].removeEventListener('submit', handleFormSubmission); |
| 24142 | }, 0, false); |
| 24143 | }); |
| 24144 | } |
| 24145 | |
| 24146 | var parentFormCtrl = ctrls[1] || controller.$$parentForm; |
| 24147 | parentFormCtrl.$addControl(controller); |
| 24148 | |
| 24149 | var setter = nameAttr ? getSetter(controller.$name) : noop; |
| 24150 | |
| 24151 | if (nameAttr) { |
| 24152 | setter(scope, controller); |
| 24153 | attr.$observe(nameAttr, function(newValue) { |
| 24154 | if (controller.$name === newValue) return; |
| 24155 | setter(scope, undefined); |
| 24156 | controller.$$parentForm.$$renameControl(controller, newValue); |
| 24157 | setter = getSetter(controller.$name); |
| 24158 | setter(scope, controller); |
no test coverage detected