Angularjs Angular:重写函数以使用promise
我使用的是一个角度工厂,它从提要中检索数据并对其进行一些数据操作 在完成此数据准备之前,我想阻止我的应用程序渲染第一个视图。我的理解是,我需要为此使用承诺,然后在控制器中使用。然后调用一旦承诺解决就可以运行的函数 从外观上看,我发现在我的工厂很难实现承诺。具体地说,我不确定把延期和决议放在哪里。有没有人能考虑一下,实施这一计划的最佳方式是什么 这里是我的工作工厂,没有承诺:Angularjs Angular:重写函数以使用promise,angularjs,promise,Angularjs,Promise,我使用的是一个角度工厂,它从提要中检索数据并对其进行一些数据操作 在完成此数据准备之前,我想阻止我的应用程序渲染第一个视图。我的理解是,我需要为此使用承诺,然后在控制器中使用。然后调用一旦承诺解决就可以运行的函数 从外观上看,我发现在我的工厂很难实现承诺。具体地说,我不确定把延期和决议放在哪里。有没有人能考虑一下,实施这一计划的最佳方式是什么 这里是我的工作工厂,没有承诺: angular.module('MyApp.DataHandler', []) // So Modular, much n
angular.module('MyApp.DataHandler', []) // So Modular, much name
.factory('DataHandler', function ($rootScope, $state, StorageHandler) {
var obj = {
InitData : function() {
StorageHandler.defaultConfig = {clientName:'test_feed'};
StorageHandler.prepData = function(data) {
var i = 0;
var maps = StorageHandler.dataMap;
i = data.line_up.length;
while(i--) {
// Do loads of string manipulations here
}
return data;
}
// Check for localdata
if(typeof StorageHandler.handle('localdata.favorites') == 'undefined') {
StorageHandler.handle('localdata.favorites',[]);
}
},
};
return obj;
});
下面是我通过查看示例尝试的内容:
angular.module('MyApp.DataHandler', []) // So Modular, much name
.factory('DataHandler', function ($rootScope, $q, $state, StorageHandler) {
var obj = {
InitData : function() {
var d = $q.defer(); // Set defer
StorageHandler.defaultConfig = {clientName:'test_feed'};
StorageHandler.prepData = function(data) {
var i = 0;
var maps = StorageHandler.dataMap;
i = data.line_up.length;
while(i--) {
// Do loads of string manipulations here
}
return data;
}
// Check for localdata
if(typeof StorageHandler.handle('localdata.favorites') == 'undefined') {
StorageHandler.handle('localdata.favorites',[]);
}
return d.promise; // Return promise
},
};
return obj;
});
但当我在控制器中使用此选项时,控制台中不会显示任何内容:
DataHandler.InitData()
.then(function () {
// Successful
console.log('success');
},
function () {
// failure
console.log('failure');
})
.then(function () {
// Like a Finally Clause
console.log('done');
});
有什么想法吗?就像弗洛里安提到的那样。您的异步调用在所显示的代码中并不明显 以下是您想要的要点:
angular.module("myApp",[]).factory("myFactory",function($http,$q){
return {
//$http.get returns a promise.
//which is latched onto and chained in the controller
initData: function(){
return $http.get("myurl").then(function(response){
var data = response.data;
//Do All your things...
return data;
},function(err){
//do stuff with the error..
return $q.reject(err);
//OR throw err;
//as mentioned below returning a new rejected promise is a slight anti-pattern,
//However, a practical use case could be that it would suppress logging,
//and allow specific throw/logging control where the service is implemented (controller)
});
}
}
}).controller("myCtrl",function(myFactory,$scope){
myFactory.initData().then(function(data){
$scope.myData = data;
},function(err){
//error loudly
$scope.error = err.message
})['finally'](function(){
//done.
});
});
您的
d.resolve()在哪里?您似乎没有执行任何操作。您也没有调用prepData
,感谢@calebboyd,我们将尝试实现这一点。正如您所提到的,这需要一些重构来实现。@calebboyd返回显式拒绝是一种反模式。您应该始终更喜欢抛出
ing,就像在同步代码中一样。将连续性和抛出安全性链接起来是承诺的一个重要而基本的部分。你应该在那时取代承诺。因为他们推广这种反模式。