如何在angularjs中点击服务的url?
在上面的代码中,我尝试从控制器中点击url并在页面上显示响应。现在我尝试从服务中点击url并在页面上显示响应。是否可能。有人能帮我解决这个问题吗?您的问题似乎是如何用AngularJS编写服务。请阅读以下内容: 它应该是这样的:如何在angularjs中点击服务的url?,angularjs,Angularjs,在上面的代码中,我尝试从控制器中点击url并在页面上显示响应。现在我尝试从服务中点击url并在页面上显示响应。是否可能。有人能帮我解决这个问题吗?您的问题似乎是如何用AngularJS编写服务。请阅读以下内容: 它应该是这样的: var app=angular.module('httpExample',[]); app.controller("FetchController",["$scope","$http","$templateCache",function($scope,$http,$te
var app=angular.module('httpExample',[]);
app.controller("FetchController",["$scope","$http","$templateCache",function($scope,$http,$templateCache){
var req={
method : 'GET',
url : 'http://www.w3schools.com/angular/customers.php',
cache:'templateCache',
headers: {
'Content-Type': undefined
},
};
$scope.fetch = function() {
$http(req).
success(function(response,status,headers,config)
{
$scope.data=response;
console.log(angular.toJson(response));
}).
error(function(data)
{
console.log("Error : " + data);
});
};
}]);
还有你的控制器
app.factory('FetchService', ['$http', function($http) {
return {
fetch: fetch
};
function fetch() {
// Do whatever you want.
// Return whatever you want
}
});
因此,唯一的区别是您将向控制器返回$http承诺或结果。对于您的代码,这可能是一个可行的解决方案:
app.controller('FetchController', ['FetchService', function(FetchService) {
$scope.fetch = function() {
$scope.data = FetchService.fetch();
};
});
然后,在控制器中注入“YourService”,只需调用YourService.fetch.thenfunctionresp{
};
作为一个粗糙的脚手架,这就足够了。如果您不确定注入/http承诺,请阅读有关服务和$http承诺的angular文档。这将是您的重构代码。请求的逻辑现在封装在服务中
.service('YourService', function($http){
var YourService = {
fetch: function(){
var promise = $http({method: "GET",
url: "/theurl"
})
.then(function(response){
return response;
}, function(error){
return error;
});
return promise;
}
};
return YourService;
现在我正在发布我的答案。这可能会对其他人有所帮助
var app=angular.module('httpExample',[]);
app.controller("FetchController",["$scope","someService","$templateCache",
function($scope,someService,$templateCache){
$scope.fetch = function() {
someService.hitTheUrl()
.success(function(response) {
$scope.data=response;
console.log(angular.toJson(response));
})
.error(function(data) {
console.log("Error : " + data);
});
};
}])
.factory('someService', [$http, function ($http) {
return {
hitTheUrl: function () {
var req = {
method : 'GET',
url : 'http://www.w3schools.com/angular/customers.php',
cache:'templateCache',
headers: {
'Content-Type': undefined
},
};
return $http(req);
}
};
}]);
这将在没有错误时在页面上显示url的内容。对不起,我不明白您要做什么。你所说的点击是什么意思?我的意思是,当我在控制器中使用http时,我能够在页面上显示响应。现在我尝试的是,通过在服务中使用http,我必须在页面上显示响应。上面的代码正在工作,但成功和错误也必须在服务块中,并且必须只向控制器提供响应。我已经尝试过,但在返回时我无法分析您希望使用异步操作作为同步方法调用,这不容易做到。这就是为什么使用承诺。承诺需要在控制器中处理。我们不能通过将响应分配给变量并将该变量返回控制器来将响应返回控制器吗?我们在这里使用的是当我们得到一个没有任何错误的响应时,但是如果我们得到一个错误,它如何显示…我尝试在控制器中使用error block-after-then block,但它没有得到执行确定,让我澄清一下:你的第一个函数。然后是成功。第二个可选块是错误块。下面是AngularJS文档中关于$q:var promise=asyncGreet'Robin Hood'的示例;promise.thenfunctiongreeting{alert'成功:'+greeting;},functionreason{alert'失败:'+reason;};有关更多信息,请参阅!
var app=angular.module('httpExample',[]);
app.controller("FetchController",["$scope","clickevent",function($scope,clickevent){
clickevent.fetch().error(function(error){
console.log("Error");
}).
then(function(response){
$scope.req=response;
console.log(angular.toJson(response));
});
}]);
app.service("clickevent",['$http',function($http){
var clickevent={
fetch:function(){
var prom=$http({method:"GET",url:"http://www.w3schools.com/angular/customers.php"}).
success(function(response){
return response;
}).
error(function(err){
return err;
});
return prom;
}
};
return clickevent;
}]);