【问题标题】:AngularJS JSONP not updating key value to objectAngularJS JSONP没有将键值更新为对象
【发布时间】:2017-10-17 15:51:55
【问题描述】:

我试图用//Second JSONP 回调函数更新channel.status 对象。

angular.element(document).ready(function() {
    channels = [];
    channel = {};

    followersAPI = followersAPI.replace(/theUser/, user);
    followersAPI = $sce.trustAsResourceUrl(followersAPI);

    // First JSONP
    $http.jsonp(followersAPI).then(function (response) {
        var follows = response.data.follows;

        for (var i = 0; i < follows.length; i ++) {
            var currentChannel = follows[i].channel;

            if (currentChannel.status == null) {
                currentChannel.status = "offline";
            }

            channel = {
                name: currentChannel.name,
                display_name: currentChannel.display_name,
                logo: currentChannel.logo,
                url: currentChannel.url,
                status: "loading"
            };

            streamAPI = "https://wind-bow.glitch.me/twitch-api/streams/";
            streamAPI += channel.name;
            streamAPI = $sce.trustAsResourceUrl(streamAPI);

            // Second JSONP
            $http.jsonp(streamAPI).then(function (response) {
                data = response.data;
                if (data.stream == null) {
                    channel["status"] = "offline";
                } else {
                    channel["status"] = data.stream.channel.status;
                }
            });
            channels.push(channel);
        }
    });
    $scope.channels = channels;
    console.log($scope.channels);
});

没有错误消息,但仅更新了 channels[] 数组中的最后一个 channel{} 对象。

这是channel.status 的 HTML 部分:

 <div id="channel-status" class="col-md-6">
        <a
        class="btn"
        href="{{channel.url}}"
        target="_blank">
            {{channel.status}}
        </a>
</div>

【问题讨论】:

    标签: javascript angularjs jsonp


    【解决方案1】:

    对于for-loop中的异步调用$http.jsonp,在它的回调中你只会得到最后定义的channel的实例。作为一个解决方案,你应该把所有关于channel的东西都移到Second JSONP中。

    var currentChannel = follows[i].channel;
    
    streamAPI = "https://wind-bow.glitch.me/twitch-api/streams/";
    streamAPI += channel.name;
    streamAPI = $sce.trustAsResourceUrl(streamAPI);
    
    // Second JSONP
    $http.jsonp(streamAPI).then(function (response) {
        data = response.data;
        if (currentChannel.status == null) {
            currentChannel.status = "offline";
        }
    
        channel = {
            name: currentChannel.name,
            display_name: currentChannel.display_name,
            logo: currentChannel.logo,
            url: currentChannel.url,
            status: "loading"
        };
    
        if (data.stream == null) {
            channel["status"] = "offline";
        } else {
            channel["status"] = data.stream.channel.status;
        }
        channels.push(channel);
    });
    

    【讨论】:

    • 不幸的是,我之前尝试过,但它导致了一些其他问题:如果我将channels.push(channel); 推入// Second JSONPchannel{} 中的所有其他属性都重复除了$$hashKey;如果它在外面(和在for loop 里面),它会显示Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed.
    • @hanabinoir 您可以添加track by $index 以避免该错误。
    • 它没有错误消息,但属性仍然重复。也许在for-loop 中使用jsonp 中的回调并不好。
    • @hanabinoir 如果Second JSONP 有重复结果,则会重复。 track by $index 将让 ng-repeat 忽略重复但显示所有记录。
    • 如您所说,jsonp 回调仅从 for 循环中获取最后一个定义,并将其复制。我正在寻找比 jsonpfor-loop 中更好的结构。
    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    相关资源
    最近更新 更多