Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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
Javascript 角度-在服务url中使用范围变量_Javascript_Angularjs_Angular Services - Fatal编程技术网

Javascript 角度-在服务url中使用范围变量

Javascript 角度-在服务url中使用范围变量,javascript,angularjs,angular-services,Javascript,Angularjs,Angular Services,目前我想从API获取数据,使用AngularJS服务发送一些搜索参数。在我的ng模型中,我有一个名为search的变量,我想将该变量用作API URL的参数 我的第一个(不成功)方法是直接在服务内部使用$scope.search变量: $http.get('http://www.omdbapi.com/?s=“+$scope.search+”&type=series&r=json')。然后(函数(数据){ 延迟。解析(数据); }); 我已经说过,将$scope传递给服务是不可能的(而且无论如

目前我想从API获取数据,使用AngularJS服务发送一些搜索参数。在我的ng模型中,我有一个名为search的变量,我想将该变量用作API URL的参数

我的第一个(不成功)方法是直接在服务内部使用$scope.search变量:

$http.get('http://www.omdbapi.com/?s=“+$scope.search+”&type=series&r=json')。然后(函数(数据){
延迟。解析(数据);
});
我已经说过,将$scope传递给服务是不可能的(而且无论如何也不应该这样做),因此如何在服务中使用scope变量作为参数,还有,除了添加字符串
myUrl+search
,还有没有更干净的方法来设置参数

完整代码:

myApp.service('showsService',函数($http,$q){
var deferred=$q.deferred();//承诺说“我以后会做这个”
$http.get('http://www.omdbapi.com/?s=sherlock&type=series&r=json)。然后(函数(数据){
延迟。解析(数据);
});
this.getShows=函数(){
回报。承诺;
}
});
myApp.controller(“showsController”,函数($scope,showsService){
$scope.$watch('search',function(){
fetch();
});
函数fetch(){
var promise=showsService.getShows();
promise.then(函数(数据){
$scope.showsResult=data.data.Search;//使用API返回的名称
});
}
});

只需将
搜索作为参数传递给服务函数:

myApp.service('showsService', function($http){
    this.getShows = function(search){
        var url = 'http://www.omdbapi.com/s='+search+'&type=series&r=json';
        var promise = $http.get(url);
        return promise;
    };
});
然后在控制器中:

myApp.controller("showsController", function($scope, showsService){
   $scope.$watch('search', function(value) {
      fetch(value);
   });

   function fetch(search){
       var promise = showsService.getShows(search);
       promise.then(function(response){
           $scope.showsResult = response.data.Search;
       });
    };
});
由于
$http
服务已返回承诺,因此无需使用
$q.defer
创建承诺


更新
$http
服务能够序列化参数:

myApp.service('showsService', function($http){
    this.getShows = function(search){
        //var url = 'http://www.omdbapi.com/s='+search+'&type=series&r=json';
        var url = 'http://www.omdbapi.com/'
        var config = { 
            params: { 
                s: search,
                type: 'series',
                r: 'json'
            }
        };
        var promise = $http.get(url, config);
        return promise;
    };
});

您可以直接将搜索数据传递给服务

var getShows = showsService.getShows($scope.search);
getShows.then(function(resposne){
    console.log(resposne.data);
})
服务代码

myApp.service('showsService',['$http',function commonSrv($http) {
this.getShows=function(search)
      {
        var promise = $http({
          method: 'post',
          url: 'http://www.omdbapi.com/',
          data:{"s":search, 'type':'series'},
          contentType:'application/json; charset=UTF-8',
        });
        return promise;
      };
}]);

你的问题的答案在你提到的另一个问题中。在再次提出基本相同的问题之前,您是否尝试过实现这里提供的解决方案?在给定的解决方案中,他们没有使用我正在使用的
http.get
函数,就我所见,他们也没有使用服务,因为他们实现了工厂?概念仍然相同,但您的代码在这里无论如何都是错误的。当服务初始化(应用程序启动)时,您正试图调用
$http.get
,由于服务是单例的,所以它只会调用一次。另外,
$http
返回一个承诺,因此您不应该将该承诺包装在另一个承诺中(此处不要使用
$q
)。将参数传递给
getShows(searchTerm)
,在该函数中调用
$http.get
,并将其返回到chain
。然后()
from。请使用
$http
参数
配置对象~
$http.get('http://www.omdbapi.com“,{params:{s:search,键入:'series',r:'json'}})
。将字符串连接到URL查询参数并不安全,谢谢!我从youtube上的一个视频教程中得到了使用
$q.defer
的想法,但现在我对$http的用法有了更好的理解,干杯!。我还将尝试使用@Phill建议的
params
config对象。这些教程让我想起了所有糟糕的PHP教程以及它们所造成的所有损害