MCPcopy Index your code
hub / github.com/angular-ui/ui-router / formDirectiveFactory

Function formDirectiveFactory

test/angular/1.5/angular.js:23103–23183  ·  view source on GitHub ↗
(isNgForm)

Source from the content-addressed store, hash-verified

23101 * related scope, under this name.
23102 */
23103var formDirectiveFactory = function(isNgForm) {
23104 return ['$timeout', '$parse', function($timeout, $parse) {
23105 var formDirective = {
23106 name: 'form',
23107 restrict: isNgForm ? 'EAC' : 'E',
23108 require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form
23109 controller: FormController,
23110 compile: function ngFormCompile(formElement, attr) {
23111 // Setup initial state of the control
23112 formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
23113
23114 var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
23115
23116 return {
23117 pre: function ngFormPreLink(scope, formElement, attr, ctrls) {
23118 var controller = ctrls[0];
23119
23120 // if `action` attr is not present on the form, prevent the default action (submission)
23121 if (!('action' in attr)) {
23122 // we can't use jq events because if a form is destroyed during submission the default
23123 // action is not prevented. see #1238
23124 //
23125 // IE 9 is not affected because it doesn't fire a submit event and try to do a full
23126 // page reload if the form was destroyed by submission of the form via a click handler
23127 // on a button in the form. Looks like an IE9 specific bug.
23128 var handleFormSubmission = function(event) {
23129 scope.$apply(function() {
23130 controller.$commitViewValue();
23131 controller.$setSubmitted();
23132 });
23133
23134 event.preventDefault();
23135 };
23136
23137 addEventListenerFn(formElement[0], 'submit', handleFormSubmission);
23138
23139 // unregister the preventDefault listener so that we don't not leak memory but in a
23140 // way that will achieve the prevention of the default action.
23141 formElement.on('$destroy', function() {
23142 $timeout(function() {
23143 removeEventListenerFn(formElement[0], 'submit', handleFormSubmission);
23144 }, 0, false);
23145 });
23146 }
23147
23148 var parentFormCtrl = ctrls[1] || controller.$$parentForm;
23149 parentFormCtrl.$addControl(controller);
23150
23151 var setter = nameAttr ? getSetter(controller.$name) : noop;
23152
23153 if (nameAttr) {
23154 setter(scope, controller);
23155 attr.$observe(nameAttr, function(newValue) {
23156 if (controller.$name === newValue) return;
23157 setter(scope, undefined);
23158 controller.$$parentForm.$$renameControl(controller, newValue);
23159 setter = getSetter(controller.$name);
23160 setter(scope, controller);

Callers 1

angular.jsFile · 0.70

Calls 5

addEventListenerFnFunction · 0.70
removeEventListenerFnFunction · 0.70
getSetterFunction · 0.70
extendFunction · 0.70
setterFunction · 0.50

Tested by

no test coverage detected