Angularjs:错误的$q
我在VisiteCtrl中有一个调用getItems的按钮:Angularjs:错误的$q,angularjs,q,deferred,Angularjs,Q,Deferred,我在VisiteCtrl中有一个调用getItems的按钮: <button type="button" ng-click="getItems();">Populate</button> function VisiteCtrl($scope, visiteService) { $scope.items = []; $scope.getItems = function() { var promise = visiteService.getItems();
<button type="button" ng-click="getItems();">Populate</button>
function VisiteCtrl($scope, visiteService) {
$scope.items = [];
$scope.getItems = function() {
var promise = visiteService.getItems();
promise.then(function(items) {
$scope.items = items;
});
};
}
还有我的“visiteService”:
var module = angular.module('app', []);
module.service('visiteService', function($q) {
this.getItems = function() {
var deferred, result = [];
deferred = $q.defer();
var db = window.openDatabase('database', '1.0', 'database', 200000);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS todos(id integer primary key, item text, qty integer, type varchar(50))");
/*tx.executeSql("DELETE FROM todos;");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Oignon', 1, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Viande', 2, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Dormir', 1, 'Autre')");*/
tx.executeSql("SELECT * FROM todos", [], function(tx, res) {
for(var i = 0; i < res.rows.length; i++) {
result.push({
id : res.rows.item(i).id,
item : res.rows.item(i).item,
qty : res.rows.item(i).qty,
type : res.rows.item(i).type
});
}
deferred.resolve(result);
},
function(tx, err) {
console.log(err);
});
},
function(err) {
console.log(err);
});
return deferred.promise;
}
});
第一次点击:我有臀部
第二次点击:我有跳跃
就像第二次单击resolve de$q一样
我完全迷路了。你不知道吗
提前感谢您的回答。我不确定它是否能帮助您复制/粘贴可能是错误的,但您的服务中有一个错误功能太多了:
Calling $q.when takes a promise or any other type, if it is not a promise then it will
wrap it in a promise and call resolve. If you pass a value to it then it is never
going to be rejected.
[http://plnkr.co/edit/tX29CPDMhQnYchOVPdfZ?p=preview][1]
Here is my plunker link check it
this.getItems函数不能有,functionerr{}after。我认为这可能会对退回的承诺造成问题。还有一件事,考虑添加一个延迟。如果出现错误,请拒绝“Theerroryyouwanttopass”
this.getItems = function() {
var deferred, result = [];
deferred = $q.defer();
var db = window.openDatabase('database', '1.0', 'database', 200000);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS todos(id integer primary key, item text, qty integer, type varchar(50))");
/*tx.executeSql("DELETE FROM todos;");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Oignon', 1, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Viande', 2, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Dormir', 1, 'Autre')");*/
tx.executeSql("SELECT * FROM todos", [], function(tx, res) {
for(var i = 0; i < res.rows.length; i++) {
result.push({
id : res.rows.item(i).id,
item : res.rows.item(i).item,
qty : res.rows.item(i).qty,
type : res.rows.item(i).type
});
}
deferred.resolve(result);
},
function(tx, err) {
console.log(err);
deferred.reject(err);
});
return deferred.promise;
}
谢谢大家的回答。现在它工作了 以下是修改后的VisiteService.js:
var module = angular.module('app', []);
module.service('visiteService', function($q, $rootScope) {
this.getItems = function() {
var deferred, result = [];
deferred = $q.defer();
var db = window.openDatabase('database', '1.0', 'database', 200000);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS todos(id integer primary key, item text, qty integer, type varchar(50))");
/*tx.executeSql("DELETE FROM todos;");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Oignon', 1, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Viande', 2, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Dormir', 1, 'Autre')");*/
tx.executeSql("SELECT * FROM todos", [], function(tx, res) {
for(var i = 0; i < res.rows.length; i++) {
result.push({
id : res.rows.item(i).id,
item : res.rows.item(i).item,
qty : res.rows.item(i).qty,
type : res.rows.item(i).type
});
}
deferred.resolve(result);
$rootScope.$apply()
},
function(tx, err) {
console.log(err);
});
});
return deferred.promise;
}
});
尝试在服务中使用$rootScope.$apply,以便在angular世界内部解决承诺。是的,db回调是在angular不知道的情况下发生的,$scope.$apply应该可以做到这一点。仅供参考,不需要执行$rootScope。$apply由于在$apply中,$rootScope服务会自动使用。
var module = angular.module('app', []);
module.service('visiteService', function($q, $rootScope) {
this.getItems = function() {
var deferred, result = [];
deferred = $q.defer();
var db = window.openDatabase('database', '1.0', 'database', 200000);
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS todos(id integer primary key, item text, qty integer, type varchar(50))");
/*tx.executeSql("DELETE FROM todos;");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Oignon', 1, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Viande', 2, 'Course')");
tx.executeSql("INSERT INTO todos(id, item, qty, type) VALUES (null, 'Dormir', 1, 'Autre')");*/
tx.executeSql("SELECT * FROM todos", [], function(tx, res) {
for(var i = 0; i < res.rows.length; i++) {
result.push({
id : res.rows.item(i).id,
item : res.rows.item(i).item,
qty : res.rows.item(i).qty,
type : res.rows.item(i).type
});
}
deferred.resolve(result);
$rootScope.$apply()
},
function(tx, err) {
console.log(err);
});
});
return deferred.promise;
}
});