3

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

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

3 Answers 3

1

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.

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

1 Comment

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.
0

Try:

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

1 Comment

Tried but with no luck.
0

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>
</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();
}

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.