-1
我想在Angularjs应用中实现this promise pattern。但someUserContent
返回值不是所需的实际数据值true
。如何将数据传送给控制器?Angular.then undefined
在我的控制器:
.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);
}
}
}
如果我这样做,'。然后(Myfactory.getUserContent)'产生 – lilbiscuit
self.db不null'的错误'无法读取属性 'allDocs'存在于getUserContent中,尝试使整个工厂的youre db可用 this.db = new PouchDB('users',{location:'default'}); –
不'var self = this;'照顾这个问题?更改为'this.db'不能解决该错误。 – lilbiscuit