我正在開發一個Angular應用程序。我需要在SQLite中創建一個數據庫,以便在我的Android項目中使用本地數據庫。我看到的每一個教程都教會我在工廠製作並在其他頁面中調用它。無法處理控制器中的工廠信息
問題是,在很多情況下,我需要獲取這些信息並對其進行處理。我已經能夠在表單中顯示它,但不幸的是,我無法操縱工廠返回的對象。
我的代碼如下。
廠源碼:
var db = null;
var clienteselec = [];
var sqlite = angular.module('sqlite', ['ionic', 'ngCordova']);
sqlite.run(function ($ionicPlatform, $cordovaSQLite, $window) {
$ionicPlatform.ready(function() {
db = $cordovaSQLite.openDB({ name: "rollers.db", location: 1 });
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS clientes (id integer primary key, nome varchar(40))");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS instalacao (id integer primary key, idCliente int, dataInst datetime)");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS manutencao (id integer primary key, idCliente int, idInstalacao int, dataManut datetime)");
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS equipamento (id integer primary key, idInstalacao int, idManutencao int, TC int autoincrement, posicao varcar(1), Rolo varchar(40), dataEquip datetime)");
});
})
sqlite.factory('clientesFactory', function ($cordovaSQLite, $rootScope) {
return {
insert: function (Nome) {
var query = "INSERT INTO clientes (nome) VALUES (?);";
var values = [Nome];
$cordovaSQLite.execute(db, query, values).then(
function (res) {
alert('Cliente Cadastro com Sucesso!');
$cordovaSQLite.execute(db, "SELECT max(id) as id from clientes", []).then(
function (res) {
if (res.rows.length > 0) {
var valores = res.rows.item(0);
$rootScope.idCliente = valores.id;
}
}
);
},
function (err) {
alert('Cliente não cadastrado. Estamos verificando o problema!');
}
);
},
selectTodos: function(tab){
var query = "SELECT * FROM " + tab;
clienteselec = [];
$cordovaSQLite.execute(db, query,[]).then(function (result) {
if(result.rows.length){
for (var i = 0; i < result.rows.length; i++) {
clienteselec.push(result.rows.item(i));
}
}else{
console.log("no data found!");
}
}, function(err){
console.log("error" + err);
});
},
});
控制器:
.controller('ClienteCtrl', ['clientesFactory', '$scope', '$state', '$window', '$rootScope', function (clientesFactory, $scope, $state, $window, $rootScope) {
$scope.listaClientes = function() {
clientesFactory.insert('teste');
clientesFactory.selectTodos('clientes');
$scope.seleciona = clienteselec;
}
}])
HTML:
<ion-content padding>
<div ng-controller="ClienteCtrl">
<button ng-click="listaClientes()">Novo Cliente</button>
<!--<table>
<tr ng-repeat="cli in clientes">
<td></td>
</tr>
</table>-->
<ion-item ng-repeat="cli in seleciona">
{{cli.nome}}
<button ng-click="cadastraCliente({{cli.id}})">Novo</button>
<button ng-click="instala({{cli.id}}, {{cli.nome}})">Instalação</button>
<button ng-click="excluiCliente({{cli.id}}, {{cli.nome}})">Excluir</button>
</div>
</ion-content>
誰能幫助?
非常感謝您的幫助gvsrepins。我會測試你放在那裏的承諾,如果它有效的話,會作出迴應。 – FelipeF
我很樂意幫助@FelipeF。 :)如果此答案符合您的需求,請將其標記爲您的問題的答案。謝謝。 – gvsrepins