MCPcopy
hub / github.com/angular-ui/ui-grid / formDirectiveFactory

Function formDirectiveFactory

lib/test/angular/1.6.7/angular.js:23774–23854  ·  view source on GitHub ↗
(isNgForm)

Source from the content-addressed store, hash-verified

23772 * related scope, under this name.
23773 */
23774var formDirectiveFactory = function(isNgForm) {
23775 return ['$timeout', '$parse', function($timeout, $parse) {
23776 var formDirective = {
23777 name: 'form',
23778 restrict: isNgForm ? 'EAC' : 'E',
23779 require: ['form', '^^?form'], //first is the form's own ctrl, second is an optional parent form
23780 controller: FormController,
23781 compile: function ngFormCompile(formElement, attr) {
23782 // Setup initial state of the control
23783 formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
23784
23785 var nameAttr = attr.name ? 'name' : (isNgForm && attr.ngForm ? 'ngForm' : false);
23786
23787 return {
23788 pre: function ngFormPreLink(scope, formElement, attr, ctrls) {
23789 var controller = ctrls[0];
23790
23791 // if `action` attr is not present on the form, prevent the default action (submission)
23792 if (!('action' in attr)) {
23793 // we can't use jq events because if a form is destroyed during submission the default
23794 // action is not prevented. see #1238
23795 //
23796 // IE 9 is not affected because it doesn't fire a submit event and try to do a full
23797 // page reload if the form was destroyed by submission of the form via a click handler
23798 // on a button in the form. Looks like an IE9 specific bug.
23799 var handleFormSubmission = function(event) {
23800 scope.$apply(function() {
23801 controller.$commitViewValue();
23802 controller.$setSubmitted();
23803 });
23804
23805 event.preventDefault();
23806 };
23807
23808 formElement[0].addEventListener('submit', handleFormSubmission);
23809
23810 // unregister the preventDefault listener so that we don't not leak memory but in a
23811 // way that will achieve the prevention of the default action.
23812 formElement.on('$destroy', function() {
23813 $timeout(function() {
23814 formElement[0].removeEventListener('submit', handleFormSubmission);
23815 }, 0, false);
23816 });
23817 }
23818
23819 var parentFormCtrl = ctrls[1] || controller.$$parentForm;
23820 parentFormCtrl.$addControl(controller);
23821
23822 var setter = nameAttr ? getSetter(controller.$name) : noop;
23823
23824 if (nameAttr) {
23825 setter(scope, controller);
23826 attr.$observe(nameAttr, function(newValue) {
23827 if (controller.$name === newValue) return;
23828 setter(scope, undefined);
23829 controller.$$parentForm.$$renameControl(controller, newValue);
23830 setter = getSetter(controller.$name);
23831 setter(scope, controller);

Callers 1

angular.jsFile · 0.70

Calls 3

setterFunction · 0.85
getSetterFunction · 0.70
extendFunction · 0.70

Tested by

no test coverage detected