0

This is the scenario

angular.module('myApp', [])
.controller('bookCtrl', function($scope) {
    $scope.book = {
        name: 'Session Invites',
        friends: [
             {'id': 1, 'name': 'raju' },
            {'id': 2, 'name': 'radha' },
            {'id': 3, 'name': 'luttappi' },
            ]
    };
    $scope.update = function(){ 
        $scope.book.friends[1] = {'id': 2, 'name': 'sam' };
        alert($scope.book.friends[1].name );
    };
});
<div ng-controller="bookCtrl">
    <input type="text" ng-model="book.friends[1].name"/>
    <input type="button" id="btn" name="btn" value="update" ng-click="update()"/>
</div>

I would like to use "id" instead of array "index". fiddle

<input type="text" ng-model="book.friends[1].name"/>
5
  • you can use angular.forEach to iterate and then match the id after iterating the array Commented Apr 7, 2015 at 10:22
  • @Anita - i would like to bind the array element directly using ng-model, will that be possible? Commented Apr 7, 2015 at 10:24
  • I dont think that its possible. Commented Apr 7, 2015 at 10:29
  • Look at this fiddle Commented Apr 7, 2015 at 10:35
  • See this stackoverflow.com/questions/26741166/… Commented Apr 7, 2015 at 10:45

3 Answers 3

2

You can do that in this way:

angular.module('myApp', [])
    .controller('bookCtrl', function ($scope) {
    $scope.book = {
        name: 'Session Invites',
        friends: [{
            'id': 1,
                'name': 'raju'
        }, {
            'id': 2,
                'name': 'radha'
        }, {
            'id': 3,
                'name': 'luttappi'
        }]
    };
    $scope.update = function (friend) {

        alert(friend.name);
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="bookCtrl">
    <div ng-repeat="friend in book.friends">
      <input type="text" ng-model="friend.name" />
      <input type="button" id="btn" name="btn" value="update" ng-click="update(friend)" />
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

i would like to avoid ng-repeat since textboxes will be placed in different locations in the form, it is not going to be a list.
How to submit all values with single save button?
1

It will be possible with a for loop but, I think this work around below maybe interesting for you.

Just change the structure of your JSON, using Object.proerty instead of an array. It will do the tricks.

angular.module('myApp', [])
.controller('bookCtrl', function($scope) {
    $scope.book = {
        name: 'Session Invites',
        friends: {
            id1: {'name': 'raju' },
            id2: {'name': 'radha' },
            id3: {'name': 'luttappi' },
        }
    };
    $scope.update = function(){ 
        $scope.book.friends.id1 = {'name': 'sam' };
        alert($scope.book.id1.name );
    };
});

NOTE: There's a concern about this workaround is the API providing the JSON. If the data recived structure fixed and angular need to convert JSON to this structure. You might need to aware of how huge is it the data that going to be converted. Because such task like this will cause Memory Leaked then JavaScript Garbage Collection. And it will slow down your app significantly

2 Comments

this seems to be a good approach. friends list is a dynamic one, let me see how to load that in this format.
@CoderHawk Yeap, there're only concern about this workaround is the API providing the JSON. If the data recived structure fixed and angular need to convert JSON to this structure. You might need to aware of how huge is it the data that going to be converted. Because such task like this will cause Memory Leaked then JavaScript Garbage Collection. And it will slow down your app significantly. EDIT: I will update this in the answer for the future use.
-1

You can use this instead:

    angular.module('myApp', [])
    .controller('bookCtrl', function($scope) {
    $scope.GetOne={};
        $scope.book = {
            name: 'Session Invites',
            friends: [
                 {'id': 1, 'name': 'raju' },
                {'id': 2, 'name': 'radha' },
                {'id': 3, 'name': 'luttappi' },
                ]
        };
        $scope.update = function(){ 
            $scope.GetOne=$scope.book.friends[1];
            alert($scope.GetOne.name );
        };
    });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="bookCtrl">
      <input type="text" ng-model="GetOne.name" />
      <input type="button" id="btn" name="btn" value="update" ng-click="update()" />
    </div>
  </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.