MCPcopy Create free account
hub / github.com/angular-ui/ui-router / formDirectiveFactory

Function formDirectiveFactory

test/angular/1.3/angular.js:18834–18901  ·  view source on GitHub ↗
(isNgForm)

Source from the content-addressed store, hash-verified

18832 * related scope, under this name.
18833 */
18834var formDirectiveFactory = function(isNgForm) {
18835 return ['$timeout', function($timeout) {
18836 var formDirective = {
18837 name: 'form',
18838 restrict: isNgForm ? 'EAC' : 'E',
18839 controller: FormController,
18840 compile: function ngFormCompile(formElement, attr) {
18841 // Setup initial state of the control
18842 formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
18843
18844 var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
18845
18846 return {
18847 pre: function ngFormPreLink(scope, formElement, attr, controller) {
18848 // if `action` attr is not present on the form, prevent the default action (submission)
18849 if (!('action' in attr)) {
18850 // we can't use jq events because if a form is destroyed during submission the default
18851 // action is not prevented. see #1238
18852 //
18853 // IE 9 is not affected because it doesn't fire a submit event and try to do a full
18854 // page reload if the form was destroyed by submission of the form via a click handler
18855 // on a button in the form. Looks like an IE9 specific bug.
18856 var handleFormSubmission = function(event) {
18857 scope.$apply(function() {
18858 controller.$commitViewValue();
18859 controller.$setSubmitted();
18860 });
18861
18862 event.preventDefault();
18863 };
18864
18865 addEventListenerFn(formElement[0], 'submit', handleFormSubmission);
18866
18867 // unregister the preventDefault listener so that we don't not leak memory but in a
18868 // way that will achieve the prevention of the default action.
18869 formElement.on('$destroy', function() {
18870 $timeout(function() {
18871 removeEventListenerFn(formElement[0], 'submit', handleFormSubmission);
18872 }, 0, false);
18873 });
18874 }
18875
18876 var parentFormCtrl = controller.$$parentForm;
18877
18878 if (nameAttr) {
18879 setter(scope, null, controller.$name, controller, controller.$name);
18880 attr.$observe(nameAttr, function(newValue) {
18881 if (controller.$name === newValue) return;
18882 setter(scope, null, controller.$name, undefined, controller.$name);
18883 parentFormCtrl.$$renameControl(controller, newValue);
18884 setter(scope, null, controller.$name, controller, controller.$name);
18885 });
18886 }
18887 formElement.on('$destroy', function() {
18888 parentFormCtrl.$removeControl(controller);
18889 if (nameAttr) {
18890 setter(scope, null, attr[nameAttr], undefined, controller.$name);
18891 }

Callers 1

angular.jsFile · 0.70

Calls 4

addEventListenerFnFunction · 0.70
removeEventListenerFnFunction · 0.70
setterFunction · 0.70
extendFunction · 0.70

Tested by

no test coverage detected