监视嵌套在传递到角度组件的多个对象中的更新

Watching for updates nested within multiple objects passed into angular component

本文关键字:对象 更新 组件 嵌套 监视      更新时间:2023-09-26

我有以下角度控制器:

{ id: 1, shape: "rect", label: "Start", class: "in-progress",
        subGraph: {
                expand: true,
                nodes: [
                    {id: 10, shape: "rect", label: "Sub Process", class: "in-progress",
                        subGraph: {
                            expand: true,
                            nodes: [
                                {id: 15, label: "Beginning", shape: "rect", class: "default"},
                                {id: 16, label: "Middle", shape: "rect", class: "error"},
                                {id: 17, label: "End", shape: "rect", class: "in-progress"}
                                ],
                             edges: []
                        }},
                           {id: 11, shape: "ellipse", label: "Review", class: "error"},
                           {id: 12, shape: "ellipse", label: "Finalize", class: "default"}
                        ],
                edges: [
                    {from: 10, to: 11, type: "circle", label: "", dashed: false},
                    {from: 11, to: 12, type: "diamond", label: "", dashed: false}
                    ]
            }
    }

以下是在自定义组件之外的控制器中进行更新的功能:

 // node being updated is passed into the function below
 function onNodeUpdated (node) {
        for (var y = 0; y < this.nodes.length; y++) {
            if (this.nodes[y].id === node.id) {
                    this.nodes.splice(y, 1, angular.copy(this.nodes[y]));
                    break;
            } else if (this.nodes[y].subGraph) {
                for (var x = 0; x < this.nodes[y].subGraph.nodes.length; x++) {
                    if (this.nodes[y].subGraph.nodes[x].id === node.id) {
                        this.nodes[y].subGraph.nodes.splice(x, 1, angular.copy(this.nodes[y].subGraph.nodes[x]));
                        break;
                    }
                }
            }
        }
    };

我将节点传递到我的自定义角度组件中,如下所示:在我的HTML:中

 <example nodes="ctrl.nodes" edges="ctrl.edges"></example>

然后在我的组件中,我在我的链接功能中观察这些节点的更新,如下所示:

  scope.$watchCollection("ctrl.nodes", function() {
                renderGraph();
    });

上面观察节点的变化,即节点的不同形状、添加的类或更改的节点标签。检测到后,图形将重新渲染,显示对节点的更改。这适用于顶级节点,即下面id为#1的节点,但不适用于子Graph中的嵌套节点。

我的问题是,我如何才能开始侦听对嵌套边和节点的更改/更新——我的节点中id为1的所有内容。我还没有看到一个行之有效的例子。所有更新都发生在"示例"组件之外。组件内部唯一发生的事情是,新的节点和/或边阵列被传入时,手表被触发

我试过:

scope.$watch("ctrl.nodes", function() {
             renderGraph();
}, true);
 scope.$watchCollection("ctrl.nodes.subGraph.nodes", function() {
              renderGraph();
});

两者都尝试从不调用组件内部的$watch,需要它来重新渲染节点和边更新。重要的一点是,我不知道组件的用户会有多少子图,所以它需要是动态的,以说明"n"个子图级别。这可能吗?感谢

$scope.$watch(
function(){ return ctrl.nodes },
function(newVal, oldVal){
             renderGraph();
}, true);

$scope.$watch(
    function(){ return ctrl.nodes.subGraph.nodes },
    function(newVal, oldVal){
                 renderGraph();
    }, true);

此外,如果此代码在示例指令中,则可以绑定到指令中作用域上的数据。

使用这种语法,你几乎可以对任何事情进行观察。即服务等。