更新范围内部对象

时间:2015-05-20 22:00:38

标签: javascript angularjs angularjs-scope

我有一个复杂的模型来代表足球锦标赛的简化版本,如下所示:

fire_df2<-fire_df[order(fire_df$rows, fire_df$cols, fire_df$date),]
fire_ID=numeric(length=nrow(fire_df2))
fire_ID[1]=1
for (i in 2:nrow(fire_df2)){
fire_ID[i]=ifelse(
fire_df2$rows[i]-fire_df2$rows[i-1]<=abs(1) & fire_df2$cols[i]-fire_df2$cols[i-1]<=abs(1) & fire_df2$date[i]-fire_df2$date[i-1]<=abs(4),
fire_ID[i-1],
i)
}
length(unique(fire_ID))
fire_df2$fire_ID<-fire_ID

为了简单起见我仅针对第一组显示每个匹配的当前结果。每个匹配都是可点击的,并打开一个模式,用户可以在其中更改匹配结果:

var model = {
    groups: [
         {
             name: "group A",
             matchList: [
                 {id: "1", name: "Team 1 vs Team 2", score: "0-0"},
                 {id: "2", name: "Team 3 vs Team 4", score: "0-0"},
                 {id: "3", name: "Team 5 vs Team 6", score: "0-0"},
                 {id: "4", name: "Team 7 vs Team 8", score: "0-0"},
             ]
         },{
             name: "group B",
             matchList: [
                 ...
             ]
         },
         ... and so on ...
    ]
}

此表在其控制器内部具有以下范围:

<table>
    <tr ng-repeat="match in matchList" ng-click="editMatchScore(match)">
        <td>{{match.name}}</td>
        <td>{{match.score}}</td>
    </tr>
</table>

一切正常,我的模态返回一个带有用户输入匹配结果的新对象,例如:

$scope.matchList = model.groups[0];

$scope.editMatchScore = function(selectedMatch){
    // Here, I open the modal where the user can change the match result.
}

现在我想更新原始模型,以便自动更新包含组结果的视图。

如何扫描当前范围以找到正确的匹配并进行更新?

这是我到目前为止所尝试的(使用 lodash 来提取对象),此代码在关闭模式并返回newResult后启动:

{newResult: "5-0"} //This object is successfully returned by the modal

我希望var group = _.find($scope.groups, function(current){ return current.name === "group A"; // get the first group }); var match = _.find(group, function(current){ return current.id === "1"; // Assume I'm editing the first match at the moment }); match.score = "5-0"; 能够引用原始模型的匹配内部对象,因此我希望我可以编辑它并看到更改反映到原始模型,但不幸的是它不是按预期工作。我可能错过了什么,有人可以帮忙吗?

希望我很清楚,谢谢

1 个答案:

答案 0 :(得分:1)

Angular-UI $modal提供.result - 这是对结果的承诺。所以,既然你有最初选择的&#34;匹配&#34;对象,只需修改它:

$scope.editMatchScore = function(selectedMatch){
    // Here, I open the modal where the user can change the match result.
    var modalInstance = $modal.open({...});

    modalInstance.result.then(function (newScore) {
      selectedMatch.score = newScore;
    });
}