【问题标题】:Angular 1.2 to 1.3 (or newer) form removeControll and addControll issuesAngular 1.2 到 1.3(或更高版本)形成 removeControll 和 addControll 问题
【发布时间】:2016-03-22 09:13:30
【问题描述】:

我正在将应用程序从 Angular 1.2 迁移到 1.3。而且我面临着 removeControll 和 addControll 函数的不同行为的问题。

我有一个这样的指令,它在表单中转义已注册的元素(不要问为什么,我们只需要它)。

<div name="es.caped" ng-model="es.caped" esc-dir></div>    

link: function link($scope, $element, $attrs, $ctrl) {
      var nameAttr = $attrs.name.replace(/([ #;?&,.+*~\':"!^$[\]()=>|\/@\{\}])/g, '\\$1');
      $ctrl[1].$removeControl($ctrl[0]);
      $ctrl[0].$name = nameAttr;
      $ctrl[1].$addControl($ctrl[0]);
    }

Angular 1.2 会给出想要的结果{"es\\.caped":{}}。 但是 1.3 会给我旧值{"es.caped":{}}

有关详细信息,请参阅它的工作原理:
1.2 版本 - Plunker 1.2 version
1.3 版本 - Plunker 1.3 version
Plunker 会将结果形式打印为 json(1.3 输出更大,但您可以找到值)。

我尝试在文档中搜索,1.2 和 1.3 版本几乎没有,但我发现 1.5:

Note that the current state of the control will not be reflected on the new parent form. This is not an issue with normal use, as freshly compiled and linked controls are in a $pristine state. However, if the method is used programmatically, for example by adding dynamically created controls, or controls that have been previously removed without destroying their corresponding DOM element, it's the developers responsibility to make sure the current state propagates to the parent form

我已经花了很多时间来解决这个问题,但没有成功。我不知道如何传播更改。我会很高兴得到任何帮助。

注意:我也尝试使用 angular 1.4 和 1.5。它具有与 1.3 相同的行为。我的最终迁移状态是 1.5,但我正在逐个版本地遵循迁移指南。

【问题讨论】:

    标签: javascript angularjs migration


    【解决方案1】:

    这个问题可以解决。它将更改元素的名称并将其反映在控制器中。

    link: function link($scope, $element, $attrs, $ctrl) {
          var nameAttr = $attrs.name.replace(/([ #;?&,.+*~\':"!^$[\]()=>|\/@\{\}])/g, '\\$1');
          $attrs.name = nameAttr ;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 2018-06-06
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      相关资源
      最近更新 更多