Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I’m performing a $http request to the 500px API for a set of popular photos. The response object is being returned successfully, and I’m having trouble pushing the returned photo items to the view.

My controller code looks like this:

meanApp.controller 'SearchController', ['$scope', '$http', 'Global', ($scope, $http, Global) ->

  $scope.global = Global
  $scope.photos = []

  $scope.submit = ->
    if $scope.text

      $http.get("https://api.500px.com/v1/photos?feature=popular").success (data) ->
        $scope.photos.push data

]

The response object (as JSON) looks like this (trimmed down for brevity):

{
  "current_page":1,
  "total_pages":1449,
  "total_items":7244,
  "photos":[
    {
      "id":58494612,
      "user_id":1795149,
      "name":"Van Gogh!!!!"
    },
    {
      "id":49566952,
      "user_id":1795149,
      "name":"Autumn touch!"
    },
    {
      "id":49527034,
      "user_id":2670757,
      "name":"Untitled"
    },
    {
      "id":39374598,
      "user_id":3669660,
      "name":"The Wild Cannot Be Tamed Nor The Rednecks in it! "
    },
    {
      "id":28303657,
      "user_id":2843749,
      "name":"Main road to go to the moon"
    }
  ]
}

My view looks like this:

<h1>Photo search</h1>

<form ng-submit="submit()" ng-controller="SearchController">
Enter text and hit enter:
  <input type="text" ng-model="text" name="text">
  <input type="submit" id="submit" value="Submit">
</form>

<ul>
  <li ng-repeat="photo in photos.photos">{{photo.name}}</li>
  <li ng-hide="photos.length">No photos</li>
</ul>

The current behaviour is that the <ul> does not update. The expected behaviour is for the photo items to push to the <li>s. I have read about potentially needing to use $apply, but this hasn't given me any luck either.

share|improve this question
    
Try $scope.photos.push data.data – Marc Kline May 24 '14 at 1:26
    
Unfortunately that didn't seem to do it, and console.log(data.data) is giving me undefined. – gosseti May 24 '14 at 1:42
up vote 2 down vote accepted

Update

The problem is that your ng-repeat is outside of the scope of SearchController

<form ng-submit="submit()" ng-controller="SearchController">
Enter text and hit enter:
  <input type="text" ng-model="text" name="text">
  <input type="submit" id="submit" value="Submit">
</form> <!-- end SearchController -->

<ul>
  <!-- Outside of SearchController's scope -->
  <li ng-repeat="photo in photos.photos">{{photo.name}}</li> 
  <li ng-hide="photos.length">No photos</li>
</ul>

so it doesn't have access to the value of photos.photos.

As a solution, you can wrap your entire "Photo search" section, including the search and the results, under the scope of one controller.

Demo

You will probably also have to consider my previous response ...

Previous response

It seems that following the response to your request, by pushing the results to $scope.photos as an array, its contents might look like:

[{ current_page: ..., photos: [...], total_items: ..., totalpages: ...}]

If that's the case, you need to access it with an expression that looks like:

<li ng-repeat="photo in photos[0].photos">

Perhaps though you're really meaning to assign data to $scope.photos like

$scope.photos = data

In this case, your expression might work as you currently have it.

share|improve this answer
    
I didn't have any luck trying your suggestions (in a combination of both too). Could it be related to the fact that each photo in photos: [...] has an index? – gosseti May 24 '14 at 2:51
    
I thought photos: [] was an array. Sounds like it's an object with numeric keys? Can you replace the abbreviated response in your question with a full JSON output? – Marc Kline May 24 '14 at 2:54
    
Looks like I’ve confused myself a bit here. I've run console.log(angular.toJson(data)) and have updated the response from that above. – gosseti May 24 '14 at 3:11

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.