w3schools form dirty custom angularjs angularjs-directive angularjs-ng-form

angularjs - dirty - ¿Por qué se usa una función "preLink" en la directiva angular `ngForm`, en lugar de la función regular" postLink "?



reset form angularjs (1)

Dentro de angular.js, en la definición de directiva ngForm (form), la función de compilación devuelve solo una función preLink . ¿Por qué debería ser preLink lugar de postLink común?

El siguiente código es de la rama principal de angular.js:

var formDirective = { name: ''form'', restrict: isNgForm ? ''EAC'' : ''E'', controller: FormController, compile: function ngFormCompile(formElement) { // Setup initial state of the control formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS); return { pre: function ngFormPreLink(scope, formElement, attr, controller) { // if `action` attr is not present on the form, prevent the default action (submission) if (!(''action'' in attr)) { // we can''t use jq events because if a form is destroyed during submission the default // action is not prevented. see #1238 // // IE 9 is not affected because it doesn''t fire a submit event and try to do a full // page reload if the form was destroyed by submission of the form via a click handler // on a button in the form. Looks like an IE9 specific bug. var handleFormSubmission = function(event) { scope.$apply(function() { controller.$commitViewValue(); controller.$setSubmitted(); }); event.preventDefault(); }; ...


La función de enlace previo se ejecuta antes que las directivas secundarias, por lo que es un buen lugar para preparar los datos que utilizarán las directivas secundarias. Supongo que en este caso prepara el controlador de envío en caso de que una directiva secundaria envíe el formulario en su función de enlace posterior.

En la práctica, el orden de ejecución de las funciones de enlace es:

  1. pre enlace de padres
  2. enlace previo del niño
  3. hijo post-link
  4. padre post-enlace