Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS Ajax获取问题_Ajax_Angularjs - Fatal编程技术网

AngularJS Ajax获取问题

AngularJS Ajax获取问题,ajax,angularjs,Ajax,Angularjs,我是一个有点新的角度,我抓挠我的头在这一个。我的工厂console.log显示,第一次所有操作都已正确定义,但由于某种原因,工厂正在重新评估我传递给未定义的的数据。以下是我的代码,从html输入开始,将数据传递到控制器,然后再传递到工厂: HTML: 以下是在输入字段中键入时按下enter键后,我在控制台开发工具中看到的输出: 第一个控制台日志console.log(通道)控制台记录正确的响应。如果我键入“freddiew”并按enter键,则会记录这些内容 然后我将其传递到我的工厂ytVids

我是一个有点新的角度,我抓挠我的头在这一个。我的工厂
console.log
显示,第一次所有操作都已正确定义,但由于某种原因,工厂正在重新评估我传递给
未定义的
的数据。以下是我的代码,从html输入开始,将数据传递到控制器,然后再传递到工厂:

HTML:

以下是在输入字段中键入时按下enter键后,我在控制台开发工具中看到的输出:

第一个控制台日志
console.log(通道)控制台记录正确的响应。如果我键入“freddiew”并按enter键,则会记录这些内容

然后我将其传递到我的工厂
ytVids.getChannel(channel)和控制台日志
console.log('name:'+name)这将记录正确的响应,如
name:freddiew

然后我声明了一个变量
var url=
,并给它一个youtube url和一个频道名

然后,我将控制台记录变量,并获得预期的
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=freddiew&key=[我的api密钥]”

当我在《邮递员》中尝试过这个url,并且只是在其中硬编码了一个名字时,这个url就起作用了。但输入名称后,控制台会弹出更多的日志,我不知道为什么会这样,看起来它覆盖了我刚才创建的用于调用的url,因为这是从工厂记录的内容:

name:freddiew

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=freddiew&key=[我的钥匙]

然后将其记录为
undefined
,api调用使用url连接
undefined
,并使用此
undefined
url进行api调用

名称:未定义

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=undefined&key=[我的钥匙]

forUsername=
是频道名称的位置


有人知道为什么它被定义为未定义的吗?

我想你打错了额外的服务电话

  .controller('indexCtrl', function ($scope, ytVids) {
     $scope.cname = function(channel){
        console.log(channel);
        //ytVids.getChannel(channel); <-- you should remove this & passed channel param in below call
        ytVids.getChannel(channel).success(function(response){
        console.log(response);
        console.log(response.items[0].contentDetails.relatedPlaylists.uploads);
        ytVids.getVids(response.items[0].contentDetails.relatedPlaylists.uploads)
        .success(function(data){
                console.log(data.items);
                $scope.ytChannel = data.items;
            });
        });
    };
  });
.controller('indexCtrl',函数($scope,ytVids){
$scope.cname=函数(通道){
控制台日志(通道);

//ytVids.getChannel(频道);我甚至都没意识到!谢谢你看到这一点。这可以解释为什么它会两次叫它……@Chipe很高兴帮助你,谢谢:-)
angular.module('youtubePlaylistAppApp')
  .factory('ytVids', function($http){

    return{
        getChannel: function(name){
            console.log('name: '+name);
            var url = 'https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername='+ name +'&key=[my api key]';
            console.log(url);
            return $http.get(url);
        },
        getVids: function(channel){
            console.log('channel: '+ channel);
            return     $http.get('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId='+channel+'&key=[my api key]')
            .success(function(response){
                //console.log(response.items);

                return response.items;
            });

        }
    };
  })
  .controller('indexCtrl', function ($scope, ytVids) {
     $scope.cname = function(channel){
        console.log(channel);
        ytVids.getChannel(channel);
        ytVids.getChannel().success(function(response){
        console.log(response);
        console.log(response.items[0].contentDetails.relatedPlaylists.uploads);
        ytVids.getVids(response.items[0].contentDetails.relatedPlaylists.uploads)
        .success(function(data){
                console.log(data.items);
                $scope.ytChannel = data.items;
            });
        });
    };
  });
  .controller('indexCtrl', function ($scope, ytVids) {
     $scope.cname = function(channel){
        console.log(channel);
        //ytVids.getChannel(channel); <-- you should remove this & passed channel param in below call
        ytVids.getChannel(channel).success(function(response){
        console.log(response);
        console.log(response.items[0].contentDetails.relatedPlaylists.uploads);
        ytVids.getVids(response.items[0].contentDetails.relatedPlaylists.uploads)
        .success(function(data){
                console.log(data.items);
                $scope.ytChannel = data.items;
            });
        });
    };
  });