为什么" $ P $砰砰"函数是在角度指令`ngForm`使用,而不是常规的" postLink"功能? [英] Why a "preLink" function is used in angular directive `ngForm`, instead of regular "postLink" function?

查看:179
本文介绍了为什么" $ P $砰砰"函数是在角度指令`ngForm`使用,而不是常规的" postLink"功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

里面angular.js,在ngForm(形式)指令的定义,编译函数仅返回 $ P $砰砰功能。为什么它应该是 $ P $砰砰,而不是常见的 postLink

以下code是angular.js主分支:

  VAR formDirective = {
  名称:'形式',
  限制:isNgForm? EAC':'E',
  控制器:的FormController,
  编译:功能ngFormCompile(formElement){
    //设置控件的初始状态
    formElement.addClass(PRISTINE_CLASS).addClass(VALID_CLASS);
    返回{
      pre:功能ngForm $ P $砰砰(范围,formElement,ATTR,控制器){
        //如果`action` attr为没有在表格上present,prevent默认操作(提交)
        如果(!(在ATTR'行动')){
          //我们不能用JQ事件,因为如果表单提交的默认期间被毁
          //动作不pvented $ P $。见#1238
          //
          // IE 9不会受到影响,因为它不火提交事件,并尝试做一个完整的
          //如果表单被提交表单通过点击摧毁处理页面重载
          //在表格的按钮。看起来像IE9的具体错误。
          VAR handleFormSubmission =函数(事件){
            范围。$应用(函数(){
            控制器$ commitViewValue()。
            控制器$ setSubmitted()。
          });
        。事件preventDefault();
        };
        ...


解决方案

所以这是一个好地方,prepare将由子指令中的任何数据的任何儿童指令之前执行的pre-Link功能。我presume在这种情况下,prepares的情况下的子指令提交表单在其链路交函数提交处理程序。

在实践中的链接功能执行的顺序是:


  1. 母公司pre-链接

  2. 子pre-链接

  3. 子后链接

  4. 父后链接

Inside angular.js, in ngForm (form) directive definition, compile function returns only a preLink function. Why it should be preLink instead of common postLink?

The following code is from angular.js master branch:

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();
        };
        ...

解决方案

The pre-link function is executed before any child directives so it's a good place to prepare any data to be used by child directives. I presume in this case it prepares the submission handler in case a child directive submits the form in its post-link function.

In practice the order of execution of link functions is:

  1. parent pre-link
  2. child pre-link
  3. child post-link
  4. parent post-link

这篇关于为什么" $ P $砰砰"函数是在角度指令`ngForm`使用,而不是常规的" postLink"功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆