0

HTML:

<ul>
  <li ng-repeat="comic in comiclist"> 
    <span>{{ comic.title }} : {{comic.id}}</span>
    <div >
      <img ng-src="{{comic.thumbnail.path}}.{{comic.thumbnail.extension}}"/>
    </div>          
    <div>
      <ul>
        <li ng-repeat="character in charlist + {{comic.id}}">    
          <span>{{character.name}}</span>
        </li>
      </ul> 
    </div>
  </li>
</ul>

JS with angular

var App = angular.module('MarvelApp', []);
  App.controller('MainController', function($scope,$window, $http) {
      $scope.GetSeries = function(){
        $http.get('http://gateway.marvel.com/v1/public/series?')
        .then(function(response){
        $scope.serieslist = response.data.data.results;
        });
      };
    $scope.GetComics = function(){
      $http.get('http://gateway.marvel.com/v1/public/series/' + $scope.selectedseries + '/comics?')
      .then(function(response){
          $scope.comiclist = response.data.data.results;
      });
      //comic in comiclist contains comic.id
      //which needs to go to GetCharacter()
    }
    $scope.GetCharacter = function(comicid){
      $http.get('http://gateway.marvel.com/v1/public/comics/' + comicid + '/characters')
      .then(function(response){
          $scope.['charlist' + comicid]  = response.data.data.results;
       //this list needs to be displayed in the second ng-repeat
      });
    };
  });

I'd like to get the character list to display in the right div. How I had it set up before, (without $scope.['charlist' + comicid]) it was overriding the other ng-repeats too.

Also, whenever GetComics() gets called it does it automatically.

1 Answer 1

1

I don't think $scope.[] is valid syntax (unless I've been missing out on a nifty trick).

You should instead name an "associative array" under $scope something like:

$scope.charlist[comicid] = ... ;

Your ng-repeat would then look something like:

<li ng-repeat="character in charlist[comic.id]">

EDIT As mentioned in the comments, $scope.charlist must be defined before the above lines can be used. This can happen in a few ways:

  1. Make a check before you set it: if(!$scope.charlist) $scope.charlist = [];

  2. Define it somewhere in the controller with $scope.charlist = [];

Of course there's any number of ways you can do this, but these make these I believe make the most sense, the first of which catches invalid states i.e. if for some reason $scope.charlist is set to null it would create a new "associative array".

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

2 Comments

"angular.js:13920 TypeError: Cannot set property '60151' of undefined" is this the error i get now on $scope.charlist[comicid] = ...; 60151 being the comicid
Technically, JavaScript doesn't have associative arrays. Think of it as an (array) object that needs instantiation. Add the line "$scope.charlist = [];" at the top of your controller. Then later references to $scope.charlist[comicid] = .... should work.

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.