2015-05-20 147 views
0

我有一个复杂的模型来表示足球锦标赛的简化版本,这样的事情:更新范围内对象

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. 
} 

一切正常,我的情态回报与用户inputed匹配结果的新的对象,例如:

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

现在,我想更新原始模型,以便自动地更新所述含第视图e组结果也是如此。

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

这是我到目前为止已经试过(使用lodash提取物),模态关闭此代码后启动并返回newResult:

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"; 

我希望match会保留对原始模型的匹配内部对象的引用,因此我希望我可以对其进行编辑并查看反映到原始模型的更改,但不幸的是,它不能按预期工作。我可能错过了一些东西,任何人都可以帮忙?

希望我是很清晰,由于

+0

:那么,既然你有参考最初选择“匹配”的对象,只需要修改呢? –

+0

@NewDev是的,我遵循这个例子:http://angular-ui.github.io/bootstrap/#/modal – BeNdErR

回答

1

角UI的$modal提供.result - 这是一个结果的一个承诺。您是否使用`角ui`的`$ modal`

$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; 
    }); 
} 
+0

谢谢,这就像一个魅力工作! – BeNdErR