0

I have been struggling a long way in a issue, wherein I need to update a parent obj from directive scope. I need to fetch some data using $http and fit this data against a property in original parent Obj.

However, after doing this, the view gets updated but somehow the model binded to these view become undefined. Since the view are updated with new data, somehow the models are becoming undefined after that.

Only now I came to know that, $http triggers a $digest, so I think that is the cause of my issue.

What can I do to avoid my models becoming undefined and the fetched values to remain intact in original object.

Just to make things clear, before I attach a wrking plnkr. here is what I mean:

I have a obj $scope.Obj. I have binded the input fields in directive template with this object using 2 way binding like

<input ng-model = Obj.something.something2[$index]/>

Now say I made a API call and update my something2 in $scope.Obj as:

$scope.Obj.something.something2 = APIResponse.something3

The values from new object something3 are visible on UI, but in backend after this

$scope.Obj.something.something2[$index] 

becomes undefined.

Pls suggest possible reasons for this...

Saurabh Tiwari
  • 4,632
  • 9
  • 42
  • 82
  • Please provide some code along with a better explanation of what `models become undefined` means. A demo that reproduces your issue would also help – charlietfl Nov 09 '15 at 16:59
  • Will try to provide a demo ASAP. Meanwhile, to explain, I have a obj $scope.Obj. I have binded the input fields in directive template with this object using 2 way binding like `` Now say I made a API call and update my something2 in $scope.Obj as: `$scope.Obj.something.something2 = APIResponse.something3` The values from new object something3 are visible on UI, but in backend after this `$scope.Obj.something.something2[$index]` becomes undefined. Pls suggest possible reasons for this... if this make some sense – Saurabh Tiwari Nov 09 '15 at 17:11
  • Update the question with additional details. That comment blob is very hard to read – charlietfl Nov 09 '15 at 17:12
  • updated..can you suggest now? – Saurabh Tiwari Nov 10 '15 at 02:39

1 Answers1

0

The other models are becoming undefined because you are replacing the object. Instead you should use angular.extend.

angular.extend($scope.Obj.something.something2, APIResponse.something3);

For more information see the AngularJS angular.extend API Reference.

georgeawg
  • 48,608
  • 13
  • 72
  • 95