0

I have a problem accessing to a scope variable defined in a nested $http.get. This is my controller code (it's related to a partial $routeProvider)

    //Content controller
app.controller('Content', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http) {
    $http.get('wp-json/wp/v2/posts?slug=' + $routeParams.slug).success(function(res) {
        $scope.post = res[0];
        document.querySelector('title').innerHTML =  res[0].title.rendered + ' | Going Solo ';
        console.log(res);
        $http.get('wp-json/wp/v2/posts?filter[artists]=' + $scope.post.pure_taxonomies.artists[0].slug + '&exclude=' + $scope.post.id).success(function(res) {
            if (res.length > 1) {
                $scope.related = res;               
            }
            else {
                $scope.related = res[0];                
            }
            console.log(res);
        }); 
    }).error(function(res, status) {
        if (status === 404) {
            $scope.is404 = true;
            document.querySelector('title').innerHTML = 'Page not found | Going Solo';
            $scope.errorMessage = 'Error: ' + res[0].message;
        }
    });
}]);

Basically I want to retrieve all the songs related to the one that is showed in my content controller (the first $http.get). To link all the songs I use a custom taxonomy called “artists”. Of course I need this to be asynchronous so I do a new $http.get inside the first one. In this request I filter for the taxonomy slug of the current post ($scope.post.pure_taxonomies.artists[0].slug) plus I add a filter to exclude the post itself by adding its ID. This is working correctly by looking at the console log. It returns an array(1) for the first request and an array(2) for the second requests (with the correct data).

The problem is by the time I try to access in my partials to the second $http.get. If I try this ng-repeat:

<div ng-repeat="songrel in related">
    <div ng-bind-html="songrel.title.extended"></div>
</div>

Nothing is showed. It simple doesn't enter in this cycle, so I guess he doesn't recognize data.related. What am I missing?

7
  • 1
    It can't find data.related because there's no $scope.data variable declared. Try just related. Commented May 23, 2017 at 18:29
  • Now it enters in the cycle, but my data are still not displayed. I don't get why, since the path is correct Commented May 23, 2017 at 18:40
  • basically this is the page where you can find the console.log new.wearegoingsolo.com/tracks/paperwhite-human-nature Commented May 23, 2017 at 18:48
  • you have a couple errors in your console, most notably, you're trying to log rel which doesn't exist and that is an unhandled error so it's probably the reason why it breaks Commented May 23, 2017 at 19:08
  • tryed with res but still doesn't work Commented May 23, 2017 at 19:12

1 Answer 1

1

It can't find data.related because there's no $scope.data variable declared. Try just related, i.e.

<div ng-repeat="songrel in related">
    <div ng-bind-html="songrel.title.extended"></div>
</div>
Sign up to request clarification or add additional context in comments.

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.