AngularJs return 异步工厂?
AngularJs return async factory?
我已经阅读了很多关于这个问题的答案,但我就是不明白。诺言去了哪里?我通过对云数据库的异步调用创建了一个简单的工厂:
app.factory('asyncFactory', function() {
let toController = function() {
firebase.database().ref('en').once('value') // get the array from the cloud database
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return snapshot.val(); // this returns later
});
return 47 // this returns immeadiately
};
return {
toController: toController // why is this necessary?
}
});
我从我的控制器调用它:
$scope.words = asyncFactory.toController();
console.log($scope.words);
这是回复:
如您所见,47
return 立即发送到控制器。如果我注释掉 return 47
那么工厂 returns undefined
。稍后异步数据记录但不会 return 到控制器。我每天都使用 promise,但我无法弄清楚 promise 的去向。
第二个问题:我需要 toController: toController
行吗?我可以摆脱它吗?
谢谢!
好吧,toController 正在吞噬自己的承诺。 (每当你调用 .then() 时,就意味着你在等待 promise),
试试这个
app.factory('asyncFactory', function() {
let toController = function() {
var deferred = $q.defer();
firebase.database().ref('en').once('value') // get the array from the cloud database
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return deferred.resolve(snapshot.val()); // this returns later
});
//return deferred.resolve(47) // this returns immeadiately
};
return {
toController: toController // why is this necessary?
}
});
如果你不想要这条线
return {
toController: toController // why is this necessary? }
app.factory('asyncFactory', function() {
return {
var deferred = $q.defer();
firebase.database().ref('en').once('value') // get the array from the cloud database
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return deferred.resolve(snapshot.val()); // this returns later
});
//return deferred.resolve(47) // this returns immeadiately
};
})
要在控制器中使用 firebase 调用的结果,工厂方法需要return一个承诺:
app.factory('asyncFactory', function($q) {
return {
toController: toController
};
function toController() {
var es6promise = firebase.database().ref('en').once('value');
var qPromise = $q.when(es6promise)
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return snapshot.val(); // this returns later
});
return qPromise;
};
});
因为 firebase .once
方法 return 是一个 ES6 承诺,该承诺需要通过将其转换为 $q Service promise with $q.when
来引入 AngularJS 框架。只有在 AngularJS 执行上下文中应用的操作才能受益于 AngularJS 数据绑定、异常处理、属性 监视等
在控制器中,使用 .then
method 从服务器提取 returns 之后的数据:
var qPromise = asyncFactory.toController();
qPromise.then(function(data) {
console.log(data)
$scope.words = data;
});
工厂函数立即return是一个承诺。当数据从服务器到达时,数据将被放置在$scope
.
我已经阅读了很多关于这个问题的答案,但我就是不明白。诺言去了哪里?我通过对云数据库的异步调用创建了一个简单的工厂:
app.factory('asyncFactory', function() {
let toController = function() {
firebase.database().ref('en').once('value') // get the array from the cloud database
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return snapshot.val(); // this returns later
});
return 47 // this returns immeadiately
};
return {
toController: toController // why is this necessary?
}
});
我从我的控制器调用它:
$scope.words = asyncFactory.toController();
console.log($scope.words);
这是回复:
如您所见,47
return 立即发送到控制器。如果我注释掉 return 47
那么工厂 returns undefined
。稍后异步数据记录但不会 return 到控制器。我每天都使用 promise,但我无法弄清楚 promise 的去向。
第二个问题:我需要 toController: toController
行吗?我可以摆脱它吗?
谢谢!
好吧,toController 正在吞噬自己的承诺。 (每当你调用 .then() 时,就意味着你在等待 promise), 试试这个
app.factory('asyncFactory', function() {
let toController = function() {
var deferred = $q.defer();
firebase.database().ref('en').once('value') // get the array from the cloud database
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return deferred.resolve(snapshot.val()); // this returns later
});
//return deferred.resolve(47) // this returns immeadiately
};
return {
toController: toController // why is this necessary?
}
});
如果你不想要这条线
return { toController: toController // why is this necessary? }
app.factory('asyncFactory', function() {
return {
var deferred = $q.defer();
firebase.database().ref('en').once('value') // get the array from the cloud database
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return deferred.resolve(snapshot.val()); // this returns later
});
//return deferred.resolve(47) // this returns immeadiately
};
})
要在控制器中使用 firebase 调用的结果,工厂方法需要return一个承诺:
app.factory('asyncFactory', function($q) {
return {
toController: toController
};
function toController() {
var es6promise = firebase.database().ref('en').once('value');
var qPromise = $q.when(es6promise)
.then(function(snapshot) { // take a snapshot
console.log(snapshot.val()); // read the values from the snapshot
return snapshot.val(); // this returns later
});
return qPromise;
};
});
因为 firebase .once
方法 return 是一个 ES6 承诺,该承诺需要通过将其转换为 $q Service promise with $q.when
来引入 AngularJS 框架。只有在 AngularJS 执行上下文中应用的操作才能受益于 AngularJS 数据绑定、异常处理、属性 监视等
在控制器中,使用 .then
method 从服务器提取 returns 之后的数据:
var qPromise = asyncFactory.toController();
qPromise.then(function(data) {
console.log(data)
$scope.words = data;
});
工厂函数立即return是一个承诺。当数据从服务器到达时,数据将被放置在$scope
.