我需要你的帮助。
我在.factory's
中有两个Services.js
第一个.factory
适用于数据库,第二个.factory
适用于电子邮件,文件等。
如何将值从第一个工厂传递到第二个工厂?如何从第一家工厂选择数据?
//first factory
angular.module('starter.services', ['ngCordova', 'ngSanitize', 'ngCsv'])
.factory('NotesDataService', function ($cordovaSQLite, $ionicPlatform) {
var db, dbName = "CONTACTS_DB"
function useWebSql() {
db = window.openDatabase(dbName, "1.0", "Contacts database", 200000)
console.info('Using webSql')
}
function useSqlLite() {
db = $cordovaSQLite.openDB({name: dbName, location : 1})
console.info('Using SQLITE')
}
function initDatabase(){
$cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS T_CONTACTS (id integer primary key, nom, prenom, codePostale, ville, email, portable)')
.then(function(res){
}, onErrorQuery)
}
$ionicPlatform.ready(function () {
if(window.cordova){
useSqlLite()
} else {
useWebSql()
}
initDatabase()
})
function onErrorQuery(err){
console.error(err)
}
return{
getAll: function(callback){
$ionicPlatform.ready(function () {
$cordovaSQLite.execute(db, 'SELECT * FROM T_CONTACTS').then(function (results) {
var data = []
for (i = 0, max = results.rows.length; i < max; i++) {
data.push(results.rows.item(i))
}
callback(data)
}, onErrorQuery)
})
}})
//second factory, here I need to get data from first factory
//to create a text file with the data from the database
// and attach this file to the e-mail
.factory('ContactsService', function ($ionicPlatform, $cordovaEmailComposer, $cordovaSQLite, $cordovaFile, NotesDataService) {
$ionicPlatform.ready(function () {
initCordovaEmailComposer();
})
function initCordovaEmailComposer() {
$cordovaEmailComposer.isAvailable().then(function () {
//is available
alert('avaible');
}, function () {
//not available
alert('not available');
})
}
return {
createEmail: function () {
var email = {
to: 'test@gmail.com',
cc: 'test@gmail.com',
bcc: 'test@gmail.com',
attachments: [
'file://cordova.file.externalDataDirectory/contacts.txt',
],
subject: 'Cordova Icons',
body: "Hello, mon ami",
isHtml: true
};
$cordovaEmailComposer.open(email).then(null, function () {
});
},
debugMessage: function (data) {
console.log('debug message', data);
},
createFile: function () {
var fileContacts = document.addEventListener('deviceready', function () {
NotesDataService.getAll(function (data) {
console.log(data)
return data
})
console.log('file contacts in console: ',fileContacts)
var fileName = 'contacts.txt'
var fileText = fileContacts
var filePath = cordova.file.externalDataDirectory
//CHECK file
$cordovaFile.checkFile(filePath, fileName).then(function (success) {
alert("file exist")
}, function (error) {
alert("file not exist", filePath)
//WRITE NEW FILE
$cordovaFile.writeFile(cordova.file.externalDataDirectory, fileName, fileText, true).then(function (success) {
// success
}, function (error) {
// error
});
});
})
},
}
})
感谢大家的建议和支持
答案 0 :(得分:0)
将这些工厂互相注入:
.factory('NotesDataService',['$ cordovaSQLite','$ ionicPlatform','ContactsService',函数($ cordovaSQLite,$ ionicPlatform,ContactsService)....
.factory('ContactsService',['$ ionicPlatform',.....,'NotesDataService',函数($ ionicPlatform,$ cordovaEmailComposer,$ cordovaSQLite,$ cordovaFile,NotesDataService){
现在您可以在其他工厂中使用工厂变量和功能。