Here is the Plunker: http://plnkr.co/edit/aqLnno?p=preview
I have a list of persons ($scope.persons
) to be displayed in a ng-grid. Each line has a edit button. When the user clicks on the button (ng-click="edit(row)"
) (see code below), I copy the displayed person (angular.copy(row.entity)
) and open a modal dialog. ... so far so good.
When the user clicks "Cancel" on the modal, nothing happens. When the user clicks "Save" on the model, the modified person is returned and should be "reintegrated" in $scope.persons
.
This is where the problem starts: I search in $scope.persons
to find the correct person and replace it with the person returned from the modal. The log statements show the $scope.persons
before and after the modification and everything looks good. However, the ng-grid doesn't seem to care. It still operates on a different (the old) $scope.persons
.
If I uncomment the row.entity = person
and set the row directly, everything looks fine in the ng-grid. However, as the underlying data model is not changed correctly, a resort will bring up the old (un-modified) person again.
Here is the code of my edit function:
$scope.edit = function(row) {
$modal.open({
templateUrl: 'personEdit.html',
backdrop: true,
windowClass: 'modal',
controller: 'PersonModalInstanceCtrl',
resolve: {
person: function() {
return angular.copy(row.entity);
}
}
})
.result.then(function(person) {
$log.log('Edited person: ' + JSON.stringify(person));
angular.forEach($scope.persons, function(p, index) {
$log.log('p: ' + JSON.stringify(p) + ' index: ' + index);
if (p.id == row.entity.id) {
$log.log('scope: ' + JSON.stringify($scope.persons));
$scope.persons[index] = person;
}
});
$log.log('scope: ' + JSON.stringify($scope.persons));
// row.entity = person;
}, function() {});
};
What am I doing wrong with my scopes?
Thanks for any help