-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