我希望只有在工厂函数(service(MyService.postData)
)中的所有功能都结束后才能调用MyFactory.getData
。
工厂的返回值始终为false或未定义,发送服务调用时出错:
1。首先在工厂中调用函数define,用于通过弹出模板登录用户
2. 其次,只有用户登录后,才会进行以下服务:
//My AngularJS controller is something like
myApp.controller("firstCtrl", function($scope, MyFactory, MyService) {
$scope.value = MyFactory.getData();
if($scope.value){
MyService.postData("url",params).then(function(){
//do something
});
});
//My AngularJS factory is something like
myApp.factory("MyFactory",function(){
return {
getData: function(){
//open pop up to login
//close pop up get confirmation
return true;
}
});
//My AngularJS service is something like
myApp.service("MyService",function($http){
//do something
});
答案 0 :(得分:0)
您使用的是$http
吗?
myApp.factory("MyService", ["$http", function($http){
return {
getData: function(url) {
return $http.get(url);
},
postData: function(url, payload) {
return $http.post(url, payload);
}
};
}]);
,然后强>
myService.getData(url).then(function(response) {
var payload = {
item: response.data.someItem;
};
var url2 = '/path/to/somewhere/';
myService.postData(url2, payload).then(function(response) {
alert('ok');
}).catch(function(response) {
alert('error2');
});
}).catch(function(response) {
alert('error1');
});