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;
});
});
};
});