简体   繁体   English

嵌套http get中的Angularjs作用域变量

[英]Angularjs scope variable in nested http get

I have a problem accessing to a scope variable defined in a nested $http.get. 我在访问嵌套$ http.get中定义的范围变量时遇到问题。 This is my controller code (it's related to a partial $routeProvider ) 这是我的控制器代码(与部分$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 ). 基本上,我想检索与内容控制器(第一个$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. 当然,我需要使它异步,因此我$http.get在第一个中$http.get一个新的$http.get 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. 在此请求中,我过滤了当前帖子的分类标准段( $scope.post.pure_taxonomies.artists[0].slug ),并且添加了一个过滤器以通过添加其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). 它为第一个请求返回一个数组(1),为第二个请求返回一个数组(2)(具有正确的数据)。

The problem is by the time I try to access in my partials to the second $http.get . 问题是当我尝试访问我的$http.get的第二个$http.get If I try this ng-repeat : 如果我尝试这个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 . 很简单,这个周期不会进入,所以我猜他不会识别data.related What am I missing? 我想念什么?

It can't find data.related because there's no $scope.data variable declared. 它找不到data.related因为没有声明$ scope.data变量。 Try just related , ie 尝试related ,即

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM