0

I have a requirement where I need to send modified value alone in the object.

Following is my object:

{
     "Code": 200,
     "ErrorMessage": null,
     "Result": {
         "Locations": [{
             "LocationName": "Location 1",
             "Address": "XYZ",
             "City": "Houston",
             "State": "TEXAS",
             "StateCode": "TX",
             "Zipcode": "75201"
         },
         {
             "LocationName": "Location 2",
             "Address": "ABC",
             "City": "Germantown",
             "State": "CALIFORNIA",
             "StateCode": "CA",
             "Zipcode": "90001"
         }]
     }
}

I used ng-repeat inorder to display data which has input fields. Now If I modify Location 1 in that Locations Object. I want to send only Location 1 details.

Is it possible to do that in Angular. I am new to angular.

4
  • 3
    Yes, it's possible. Please edit your question and show us what you tried. Thank you Commented Jun 12, 2017 at 12:22
  • I didn't tried anything. I don't know the way first to proceed Commented Jun 12, 2017 at 12:27
  • 1
    If you're in ng repeat, then you can pass the object to the controller, like this: <div ng-repeat="location in data.Result.Locations"> <button ng-click="save(location)"></button> </div> - This is the first step to proceed Commented Jun 12, 2017 at 12:30
  • Done. Its working. Thanks Alon Commented Jun 12, 2017 at 12:50

1 Answer 1

1

you can use ng-change to get the modified object

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.changeItem = function(item){
  console.log(item.LocationName)
}

$scope.items = {
     "Code": 200,
     "ErrorMessage": null,
     "Result": {
         "Locations": [{
             "LocationName": "Location 1",
             "Address": "XYZ",
             "City": "Houston",
             "State": "TEXAS",
             "StateCode": "TX",
             "Zipcode": "75201"
         },
         {
             "LocationName": "Location 2",
             "Address": "ABC",
             "City": "Germantown",
             "State": "CALIFORNIA",
             "StateCode": "CA",
             "Zipcode": "90001"
         }]
     }
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div ng-repeat="item in items.Result.Locations">
    <input ng-model="item.LocationName" ng-change="changeItem(item)"/>
 </div>
</div>

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

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.