Dominik Barann
Dominik Barann

Reputation: 685

Angular update object in array

I wanna update an object within an objects array. Is there another possibility than iterating over all items and update the one which is matching? Current code looks like the following:

angular.module('app').controller('MyController', function($scope) {
    $scope.object = {
        name: 'test',
        objects: [
            {id: 1, name: 'test1'},
            {id: 2, name: 'test2'}
        ]
    };

    $scope.update = function(id, data) {
        var objects = $scope.object.objects;

        for (var i = 0; i < objects.length; i++) {
            if (objects[i].id === id) {
                objects[i] = data;
                break;
            }
        }
    }
});

Upvotes: 13

Views: 78777

Answers (4)

Abhijeet
Abhijeet

Reputation: 8761

Filters that help in finding the element from the array, can also be used to update the element in the array directly. In the code below [0] --> is the object accessed directly.

Plunker Demo

$filter('filter')($scope.model, {firstName: selected})[0]

Upvotes: 7

Artiom
Artiom

Reputation: 7837

Pass the item to your update method. Take a look at sample bellow.

function MyCtrl($scope) {
  $scope.items = 
    [
      {name: 'obj1', info: {text: 'some extra info for obj1', show: true}},
      {name: 'obj2', info: {text: 'some extra info for obj2', show: false}},
    ];
  $scope.updateName = function(item, newName){
     item.name = newName;
  } 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app>
  <table ng-controller="MyCtrl" class="table table-hover table-striped">
    <tr ng-repeat="x in items">
        <td> {{ x.name }}</td>
        <td> 
           <a href="#" ng-show="!showUpdate" ng-click="someNewName = x.name; showUpdate = true">Update</a>
           <div ng-show="showUpdate" ><input type="text" ng-model="someNewName"> <input type="button" value="update" ng-click="updateName(x, someNewName); showUpdate = false;"></div>
         </td>
    </tr>

  </table>
</body>

Upvotes: 6

tokafew420
tokafew420

Reputation: 725

Going off your plunker, I would do this:

  • Change

    <a href="javascript:;" ng-click="selectSubObject(subObject.id)">Edit</a>
    

    to be

    <a href="javascript:;" ng-click="selectSubObject($index)">Edit</a>
    
  • Then use the array index within your $scope.selectSubObject method to directly access your desired element. Something like this:

    $scope.selectSubObject = function(idx) {
      $scope.selectedSubObject = angular.copy(
        $scope.selectedMainObject.subObjects[idx]
      );
    };
    

If however, you only have the id to go off of, then you can use the angular filterService to filter on the id that you want. But this will still do a loop and iterate over the array in the background.

See documentation for ngrepeat to see the variables that it exposes.

Upvotes: 2

Kursad Gulseven
Kursad Gulseven

Reputation: 2008

There are several ways to do that. Your situation is not very clear.

-> You can pass index instead of id. Then, your update function will be like:

$scope.update = function(index, data) {
    $scope.object.objects[index] = data;
};

-> You can use ng-repeat on your view and bind object properties to input elements.

<div ng-repeat="item in object.objects">
    ID: <input ng-model="item.id" /> <br/>
    Name: <input ng-model="item.name" /> <br/>
</div>

Upvotes: 7

Related Questions