简体   繁体   English

如何防止每次模型更改时触发ng-repeat(涉及Firebase / angularFire)

[英]How to prevent ng-repeat from triggering every time the model changes (involves Firebase/angularFire)

I am building the DOM using data synced/fetched from Firebase with angularFire. 我正在使用与angularFire从Firebase同步/获取的数据来构建DOM。 Every DOM element is built with this simple directive: 每个DOM元素都是使用以下简单指令构建的:

angular.module('App')
  .directive('acmsElement', function () {
    return {
      template: '<div ng-transclude></div> ',
      restrict: 'E',
      scope: {elem: '='},
      transclude: true,
      replace: true,
      link: function postLink(scope, element, attrs) {
        var el = angular.element(scope.elem.tag);
        if(scope.elem.content){
          el.text(scope.elem.content);
        }
        if(scope.elem.attributes) {
          el.attr(scope.elem.attributes);
        }
        element.append(el);
      }
    };
  });

and all elements are rendered on the website sequentially using ng-repeat and every element/model is made editable with textarea: 并使用ng-repeat依次在网站上呈现所有元素,并且每个元素/模型都可以通过textarea进行编辑:

<div ng-repeat="el in elems">
  <acms-element elem="el">
    <textarea style="width: 100%" ng-model="el.content"></textarea>
  </acms-element>
</div>

In the Firebase the nodes are stored as an array, so it is straight forward to display them in the right order using ng-repeat. 在Firebase中,节点存储为数组,因此直接使用ng-repeat以正确的顺序显示它们即可。 The problem starts when I try to edit any model bound to a textarea above, the ng-repeats its work on every keystroke. 当我尝试编辑绑定到上述文本区域的任何模型时,问题就开始了,ng在每次击键时都会重复其工作。 This causes that the focus on the textarea is lost, the page flickers as it is rendered, and most annoyingly, I can input only one character at a time before the page is re-generated. 这导致失去对文本区域的关注,页面在呈现时闪烁,而且最令人讨厌的是,在重新生成页面之前,我一次只能输入一个字符。 Not sure how to tackle the problem. 不确定如何解决该问题。
Thank you for your time, 感谢您的时间,
Jared 杰瑞德

I had a similar issue here: hanging-model-value-in-one-controller-triggers-model-update-in-others . 我在这里有一个类似的问题: 将模型值挂在一个控制器触发器中,在其他模型中更新

Every time a model object is changed the digest cycle is called on the $rootScope which will update all the scopes. 每次更改模型对象时,都会在$ rootScope上调用摘要循环,这将更新所有作用域。

A workaround would be to have two separate model variables for Firebase and actual elements so the changes in your textarea will not call the Firebase. 一种解决方法是为Firebase和实际元素使用两个单独的模型变量,以使您的文本区域中的更改不会调用Firebase。 Then have a $timeout() function to "sync" (going through properties recursively and update/delete them only if they are changed) those two model objects in the background. 然后使用$ timeout()函数在后台“同步”(递归地遍历属性,并仅在更改属性时更新/删除它们)这两个模型对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM