2015-05-20 145 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