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

Function formDirectiveFactory

test/angular/1.4/angular.js:20979–21059  ·  view source on GitHub ↗
(isNgForm)

Source from the content-addressed store, hash-verified

20977 * related scope, under this name.
20978 */
20979var formDirectiveFactory = function(isNgForm) {
20980 return ['$timeout', '$parse', function($timeout, $parse) {
20981 var formDirective = {
20982 name: 'form',
20983 restrict: isNgForm ? 'EAC' : 'E',
20984 require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form
20985 controller: FormController,
20986 compile: function ngFormCompile(formElement, attr) {
20987 // Setup initial state of the control
20988 formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
20989
20990 var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
20991
20992 return {
20993 pre: function ngFormPreLink(scope, formElement, attr, ctrls) {
20994 var controller = ctrls[0];
20995
20996 // if `action` attr is not present on the form, prevent the default action (submission)
20997 if (!('action' in attr)) {
20998 // we can't use jq events because if a form is destroyed during submission the default
20999 // action is not prevented. see #1238
21000 //
21001 // IE 9 is not affected because it doesn't fire a submit event and try to do a full
21002 // page reload if the form was destroyed by submission of the form via a click handler
21003 // on a button in the form. Looks like an IE9 specific bug.
21004 var handleFormSubmission = function(event) {
21005 scope.$apply(function() {
21006 controller.$commitViewValue();
21007 controller.$setSubmitted();
21008 });
21009
21010 event.preventDefault();
21011 };
21012
21013 addEventListenerFn(formElement[0], 'submit', handleFormSubmission);
21014
21015 // unregister the preventDefault listener so that we don't not leak memory but in a
21016 // way that will achieve the prevention of the default action.
21017 formElement.on('$destroy', function() {
21018 $timeout(function() {
21019 removeEventListenerFn(formElement[0], 'submit', handleFormSubmission);
21020 }, 0, false);
21021 });
21022 }
21023
21024 var parentFormCtrl = ctrls[1] || controller.$$parentForm;
21025 parentFormCtrl.$addControl(controller);
21026
21027 var setter = nameAttr ? getSetter(controller.$name) : noop;
21028
21029 if (nameAttr) {
21030 setter(scope, controller);
21031 attr.$observe(nameAttr, function(newValue) {
21032 if (controller.$name === newValue) return;
21033 setter(scope, undefined);
21034 controller.$$parentForm.$$renameControl(controller, newValue);
21035 setter = getSetter(controller.$name);
21036 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