【发布时间】:2016-08-13 19:01:58
【问题描述】:
我正在尝试在 Angularjs 应用程序中实现 this promise pattern。但是 someUserContent 返回值 true 而不是所需的实际数据。如何将数据发送到控制器?
在我的控制器中:
.controller('MyCtrl',function() {
MyFactory.checkUser(userId) //returns true
.then(MyFactory.prepDatabase()) //returns true
.then(Myfactory.getUserContent()) //returns a someUserData
.then(function(someUserData) {
//do some stuff with all this data
});
}
还有工厂:
.factory('MyFactory', function($q,$http....) {
MyFactory.prototype.checkUser = function(user_id) {
var self = this;
this.db = new PouchDB('users', {location: 'default'});
return $q.when(true);
}
Myfactory.prototype.getUserContent = function() {
if (!self.someUserData) {
return $q.when(self.db.allDocs({ include_docs: true}))
.then(function(docs) {
self.someUserData = docs.rows.map(function(row) {
return row.doc;
});
return $q.when(self.someUserData);
})
} else {
return $q.when(self.someUserData);
}
}
}
【问题讨论】:
标签: javascript angularjs promise angular-promise