[英]AngularJS: Sync Call the method in angular controller from javascript
我想从javascript方法的angularjs控制器中调用该方法。 所以我用下面的函数来调用。
function ValidateLogin(data) {
var currentUser = angular.element(document.getElementById('bdyIndex')).scope().SlateProject_GetCurrentUser();
if (currentUser.rows.length > 0) {
//doing the stuff
}
}
控制器如下所示
angular.module('indexApp', [])
.controller('indexController', function ($scope, $http) {
$scope.SlateProject_GetCurrentUser = function () {
var db = app.db;
db.transaction(function (tx) {
tx.executeSql("SELECT * FROM dbSlateProject WHERE ACTIVE = 1", [],
function (tx, e) {
console.log('Fetched Active Slate Project!')
return e;
},
function (tx, e) {
alert(e.message);
});
});
}
});
因此,在从“ SlateProject_GetCurrentUser”获取结果之前,将执行if条件。 因此,它给出错误消息为“无法读取未定义的属性'行'”。 我们如何通过同步调用方法。
控制器:
angular.module('indexApp', [])
.controller('indexController', function ($scope, $http) {
$scope.SlateProject_GetCurrentUser = function (callback) {
var db = app.db;
db.transaction(function (tx) {
tx.executeSql("SELECT * FROM dbSlateProject WHERE ACTIVE = 1", [],
function (tx, e) {
console.log('Fetched Active Slate Project!')
callback(tx) // is `tx` current user? replace it with current user if no
},
function (tx, e) {
alert(e.message);
});
});
}
});
您可以像这样访问它:
function ValidateLogin(data) {
var currentUser = angular.element(document.getElementById('bdyIndex')).scope().SlateProject_GetCurrentUser(function(currentUser){
if (currentUser.rows.length > 0) {
//doing the stuff
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.