有没有办法在AngularJS中强制重新加载递归ng-include

Is there a way to forcibly reload a recursive ng-include in AngularJS?

本文关键字:新加载 加载 ng-include 递归 AngularJS 有没有      更新时间:2023-09-26

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

问题是,如果我动态地更改对象的结构,视图就不会更新。ng-include似乎无法再生。

有什么方法可以强制视图中的ng-include从头开始重新工作吗?

我也遇到了同样的问题。试试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,它将再次渲染。