Edit array within another array

Asked

Viewed 87 times

2

I am new to Angularjs and I have the following situation:

I have a list of categories and, within this list of categories, there’s a product listing of that category.

Example: Smartphones > Galaxy, iPhone, etc...

The problem is that I can’t save the edit I make of that array of products that are within the category array.

I created a Codepen to illustrate this situation better.

I understand that the $scope.save is saving the table, not the content, but I have no idea how to save the content instead of the table.

I’d really like your help, thanks in advance.

1 answer

1


I create a Fork of your codepen with a possible solution.

Basically - keep a reference to the original object:

$scope.edit = function(user, i) {
    $scope.source = user; // Preservando a referência
    $scope.update = angular.copy(user);
};

And use angular.merge() to overwrite the properties of the original object with those of the modified clone:

$scope.save = function() {
    angular.merge($scope.source, $scope.update); // Atualiza o objeto referenciado
    $scope.update = ""; // reseta o update
};
  • Wow! It worked perfectly! Thanks for the guidance, I’ll take a look at this angular.merge to better understand its function. Thank you so much!!

  • I’m glad it worked, @Bernardosampaio!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.