【问题标题】:Is there a way to forcibly reload a recursive ng-include in AngularJS?有没有办法在AngularJS中强制重新加载递归ng-include?
【发布时间】:2015-06-05 18:57:16
【问题描述】:

我在我的 Angular 应用程序中使用递归脚本来输出一堆对象的表示。

问题是,如果我动态更改对象的结构,视图不会更新。 ng-include 似乎没有重新生成。

有没有办法强制ng-include 重新开始工作?

【问题讨论】:

  • 如果您从外部角度更改对象,您可能需要使用 $timeout 或 $apply 显式触发摘要循环。你试过了吗?
  • 是的,但是会收到 $apply() 已经在运行的错误。看来$apply 并没有触发ng-include 重新运行递归脚本
  • 一些简单的小提琴会有所帮助。
  • @IlyaLuzyanin 我不是在询问任何特定的代码,一般来说,你能强制ng-include 重新运行吗?

标签: javascript angularjs recursion angularjs-ng-include


【解决方案1】:

我遇到了同样的问题。试试 ui-if 指令,它解决了我的问题。

app.directive("uiIf", function () {
                return {
                    transclude: 'element',
                    priority: 1000,
                    terminal: true,
                    restrict: 'A',
                    compile: function (element, attr, linker) {
                        return function (scope, iterStartElement, attr) {
                            iterStartElement[0].doNotMove = true;
                            var expression = attr.uiIf;
                            var lastElement;
                            var lastScope;
                            scope.$watch(expression, function (newValue) {
                                if (lastElement) {
                                    lastElement.remove();
                                    lastElement = null;
                                }
                                if (lastScope) {
                                    lastScope.$destroy();
                                    lastScope = null;
                                }
                                if (newValue) {
                                    lastScope = scope.$new();
                                    linker(lastScope, function (clone) {
                                        lastElement = clone;
                                        iterStartElement.after(clone);
                                    });
                                }
                                iterStartElement.parent().trigger("$childrenChanged");
                            });
                        };
                    }
                };
            });

当该值设置为 false 时,元素将消失。如果为 true,它将再次渲染。

【讨论】:

  • 有意思,我马上试试这个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 2014-06-16
  • 1970-01-01
  • 1970-01-01
  • 2016-01-04
相关资源
最近更新 更多