Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having problems updating the view after an Array inside an Array is updated in the $scope.

First i check if the Array member already exists:

$scope.myArray = [];
if(typeof $scope.myArray[someIndex] == 'undefined') {
  $scope.myArray[someIndex] = {
    name: someName,
    data: []
  };
}

Then push to $scope.myArray[someIndex].data:

$scope.myArray[someIndex].data.push(dataContent);

At this point the view does not update.

Of course if i directly push to $scope.myArray it does. Any suggestions?

Edit: Fiddle here

share|improve this question
    
Have you tried calling $scope.$apply() afterwards? What happens then? –  furier Jun 18 at 14:45
    
I did, tried the solution in stackoverflow.com/questions/16039076/…, but i get "$digest already in progress error" –  TMichel Jun 18 at 14:50
1  
Can you set up a fiddle that shows the problem? –  Explosion Pills Jun 18 at 14:52
    
Sure, updated with the fiddle –  TMichel Jun 18 at 15:11

3 Answers 3

Try:

if(typeof $scope.myArray[someIndex] == 'undefined') {
   $scope.$eval(function(){
     $scope.myArray[someIndex] = {
       name: someName,
       data: []
     };
     $scope.myArray[someIndex].data.push(dataContent);
   });       
}
share|improve this answer
    
Tried but with no luck. –  TMichel Jun 18 at 15:12
up vote 0 down vote accepted

It was simpler than it looked.

Based on the response here i am setting an associative array which allows set string keys. If you declare your array as =[] you simply cannot set strings as keys.

So i just changed my declaration $scope.myArray=[] to $scope.myArray={} and voilà, it works.

share|improve this answer
    
lol, in your example you used someIndex which didn't imply it as being a string, If I knew that I could have told you from the start that a plain object works as a dictionary. –  furier Jun 18 at 18:10
    
i know, i know.. –  TMichel Jun 18 at 20:20

This works:

HTML,

<div ng-app>
<div ng-controller="NestedCtrl">
    <article ng-repeat="member in myArray">
        {{member.name}}
        <article ng-repeat="next in member.data">
            {{next.nested}}
        </article>
    </article>
</div>

Angular JS:

function NestedCtrl($scope) {

$scope.myArray = [];

var callMe = function(){
  if(typeof $scope.myArray[0] == 'undefined') {
      $scope.myArray[0] = {
          name: 'Hello',
          data: []
      };
  }

  $scope.myArray[0].data.push({nested : 'yay'});   
}

callMe();

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.