0

我正在使用 angular-ui-tree 制作具有记分卡节点层次结构的记分卡应用程序。我的记分卡节点通过 Web 服务层保持不变,并有自己的 ID 号来识别它们。

为了按 ID 使用记分卡节点,我实现了以下方法:

// get a node scope by a Scorecard Node ID.
function getNodeScopeByNodeID(id, nodesScope) {
    nodesScope = nodesScope || getRootNodesScope().$nodesScope;

    var nodes = nodesScope.childNodes();
    for (var i = 0; i < nodes.length; i++) {
        var result;
        if (nodes[i].$modelValue.ID == id)
            result = nodes[i];
        else {
            var subScope = nodes[i].$childNodesScope;
            if (subScope) {
                result = getNodeScopeByNodeID(id, subScope);
            }
        }
        if (result)
            return result;
    }

    return undefined;
}

这里的细节不是很重要,只是说这个方法会爬取构成树的嵌套范围,直到它找到 $modelValue(我的记分卡节点状态)具有我们正在寻找的 ID 的范围。它工作得很好,除非我做这样的事情:

// add a new objective node to the root of the scorecard.
$scope.newObjective = function () {
    var scope = getRootNodesScope();
    var rootArray = scope.$nodesScope.$modelValue;
    scorecardService.addObjective($scope.selectedScorecard, rootArray)
        .then(function (d) {
            rootArray.push(d);
            $scope.edit(getNodeScopeByNodeID(d.ID));
        })
        .catch(errorHandler);
}

注意“then”函数中的两行。我要求我的服务创建一个新节点,当我得到它时,我将它推入作为模型一部分的数组中。我希望我新插入的节点立即切换到“编辑”模式(其中显示了其中的一些表单控件。)但是在推送之后立即调用 getNodeScopeByNodeID 失败。如果我调试它,看起来范围还没有赶上模型。我的节点数组(模型)中的节点比我期望的子范围多一个。

我想我只需要给 Angular 一个机会来对模型更改做出反应,然后再开始通过范围寻找我正在寻找的东西。我怎么做?

4

0 回答 0