MCPcopy Create free account
hub / github.com/bclinkinbeard/angular / FormController

Function FormController

test/fixtures/foo.js:16122–16277  ·  view source on GitHub ↗
(element, attrs, $scope, $animate)

Source from the content-addressed store, hash-verified

16120//asks for $scope to fool the BC controller module
16121FormController.$inject = ['$element', '$attrs', '$scope', '$animate'];
16122function FormController(element, attrs, $scope, $animate) {
16123 var form = this,
16124 parentForm = element.parent().controller('form') || nullFormCtrl,
16125 invalidCount = 0, // used to easily determine if we are valid
16126 errors = form.$error = {},
16127 controls = [];
16128
16129 // init state
16130 form.$name = attrs.name || attrs.ngForm;
16131 form.$dirty = false;
16132 form.$pristine = true;
16133 form.$valid = true;
16134 form.$invalid = false;
16135
16136 parentForm.$addControl(form);
16137
16138 // Setup initial state of the control
16139 element.addClass(PRISTINE_CLASS);
16140 toggleValidCss(true);
16141
16142 // convenience method for easy toggling of classes
16143 function toggleValidCss(isValid, validationErrorKey) {
16144 validationErrorKey = validationErrorKey ? '-' + snake_case(validationErrorKey, '-') : '';
16145 $animate.removeClass(element, (isValid ? INVALID_CLASS : VALID_CLASS) + validationErrorKey);
16146 $animate.addClass(element, (isValid ? VALID_CLASS : INVALID_CLASS) + validationErrorKey);
16147 }
16148
16149 /**
16150 * @ngdoc method
16151 * @name form.FormController#$addControl
16152 *
16153 * @description
16154 * Register a control with the form.
16155 *
16156 * Input elements using ngModelController do this automatically when they are linked.
16157 */
16158 form.$addControl = function(control) {
16159 // Breaking change - before, inputs whose name was "hasOwnProperty" were quietly ignored
16160 // and not added to the scope. Now we throw an error.
16161 assertNotHasOwnProperty(control.$name, 'input');
16162 controls.push(control);
16163
16164 if (control.$name) {
16165 form[control.$name] = control;
16166 }
16167 };
16168
16169 /**
16170 * @ngdoc method
16171 * @name form.FormController#$removeControl
16172 *
16173 * @description
16174 * Deregister a control from the form.
16175 *
16176 * Input elements using ngModelController do this automatically when they are destroyed.
16177 */
16178 form.$removeControl = function(control) {
16179 if (control.$name && form[control.$name] === control) {

Callers

nothing calls this directly

Calls 5

toggleValidCssFunction · 0.85
assertNotHasOwnPropertyFunction · 0.85
forEachFunction · 0.85
arrayRemoveFunction · 0.85
includesFunction · 0.85

Tested by

no test coverage detected